Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions django_pandas/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@


def to_fields(qs, fieldnames):
fields = []
for fieldname in fieldnames:
model = qs.model
for fieldname_part in fieldname.split('__'):
field = model._meta.get_field(fieldname_part)
if field.get_internal_type() == 'ForeignKey':
model = field.rel.to
fields.append(field)
return fields
yield field


def read_frame(qs, fieldnames=(), index_col=None, coerce_float=False,
Expand Down
20 changes: 6 additions & 14 deletions django_pandas/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,17 @@ def to_timeseries(self, fieldnames=(), verbose=True,
defined in the ``__unicode__`` or ``__str__``
methods of the related class definition
"""
if index is None:
raise AssertionError('You must supply an index field')
if storage not in ('wide', 'long'):
raise AssertionError('storage must be wide or long')
assert index is not None, 'You must supply an index field'
assert storage in ('wide', 'long'), 'storage must be wide or long'
if rs_kwargs is None:
rs_kwargs = {}

if storage == 'wide':
df = self.to_dataframe(fieldnames, verbose=verbose, index=index)
else:
df = self.to_dataframe(fieldnames, verbose=verbose)
if values is None:
raise AssertionError('You must specify a values field')

if pivot_columns is None:
raise AssertionError('You must specify pivot_columns')
assert values is not None, 'You must specify a values field'
assert pivot_columns is not None, 'You must specify pivot_columns'

if isinstance(pivot_columns, (tuple, list)):
df['combined_keys'] = ''
Expand Down Expand Up @@ -190,11 +185,8 @@ def to_dataframe(self, fieldnames=(), verbose=True, index=None,

"""

df = read_frame(self, fieldnames=fieldnames, verbose=verbose,
index_col=index,
coerce_float=coerce_float)

return df
return read_frame(self, fieldnames=fieldnames, verbose=verbose,
index_col=index, coerce_float=coerce_float)


class DataFrameManager(PassThroughManager):
Expand Down
22 changes: 5 additions & 17 deletions django_pandas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,29 @@ def inner(pk_list):
for pk in pk_list]
out_dict = cache.get_many(frozenset(cache_keys))
try:
out_list = [None if k is None else out_dict[k]
for k in cache_keys]
return [None if k is None else out_dict[k] for k in cache_keys]
except KeyError:
out_dict = {
base_cache_key % obj.pk: force_text(obj)
for obj in model.objects.filter(pk__in={pk for pk in pk_list
if not isnan(pk)})}
cache.set_many(out_dict)
out_list = list(map(out_dict.get, cache_keys))
return out_list
return list(map(out_dict.get, cache_keys))

return inner


def build_update_functions(fieldnames, fields):
update_functions = []

for fieldname, field in zip(fieldnames, fields):
if field.choices:
choices = {k: force_text(v)
for k, v in field.flatchoices}
update_functions.append((fieldname,
replace_from_choices(choices)))
yield fieldname, replace_from_choices(choices)

elif field.get_internal_type() == 'ForeignKey':
update_functions.append((fieldname, replace_pk(field.rel.to)))

return update_functions
yield fieldname, replace_pk(field.rel.to)


def update_with_verbose(df, fieldnames, fields):
update_functions = build_update_functions(fieldnames, fields)

if not update_functions:
return

for fieldname, function in update_functions:
for fieldname, function in build_update_functions(fieldnames, fields):
df[fieldname] = function(df[fieldname])