Skip to content

ENH: Add 'records' outtype to to_dict method. #4936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2013
Merged

ENH: Add 'records' outtype to to_dict method. #4936

merged 1 commit into from
Sep 30, 2013

Conversation

tshauck
Copy link
Contributor

@tshauck tshauck commented Sep 22, 2013

to_json has a records type that turns a DataFrame into a

[{'a': 1, 'b': 3}, {'a': 2, 'b': 4}]

records json string. It'd be nice if you could do a similar thing but not have to go json.loads.

@jreback
Copy link
Contributor

jreback commented Sep 22, 2013

grt....can you add a release notes entry (improvements), and a mention in v0.13.0? (use this PR ref as there is not associated issue)

@tshauck
Copy link
Contributor Author

tshauck commented Sep 22, 2013

Updated the commit - let me know of anything else.

@jreback
Copy link
Contributor

jreback commented Sep 23, 2013

@jtratner @cpcloud any comments?


for expected_row, actual_row in zip(expected_records, recons_data):
for key in expected_row.keys():
self.assertEqual(expected_row[key], actual_row[key])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't correct. It's dict and list, right? If so, you need to just use assertEqual without the for loops.

This would fail to detect a failure if recons_data were shorter than expected_records, if expected_records was missing certain keys, etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point... it's a list of dicts.

How could I test if nan == nan? I wanted to use the test case used in the other to_dict outtypes, but ran into that issue when I tried assertEqual first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tm.assert_almost_equal?

@tshauck
Copy link
Contributor Author

tshauck commented Sep 24, 2013

@jtratner I updated the test to with your suggestion, seems to have done the trick.

@jreback
Copy link
Contributor

jreback commented Sep 24, 2013

ok....once more rebase and i think good 2 go (prob just release notes conflict)

@@ -983,6 +983,9 @@ def to_dict(self, outtype='dict'):
return dict((k, v.tolist()) for k, v in compat.iteritems(self))
elif outtype.lower().startswith('s'):
return dict((k, v) for k, v in compat.iteritems(self))
elif outtype.lower().startswith('r'):
return [dict((k, v) for k, v in zip(self.columns, row)) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the backslash here

@jreback jreback merged commit ca6475d into pandas-dev:master Sep 30, 2013
@jreback
Copy link
Contributor

jreback commented Sep 30, 2013

thanks!

@nesterko
Copy link

Thanks for the feature!

The resulting list of dicts has NumPy data types in entries, which is unfortunate -- difficult to serialize to JSON with known packages such as json.

Is there a chance to have an option to convert entry types to native Python for this?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants