-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
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) |
Updated the commit - let me know of anything else. |
|
||
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]) |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
@jtratner I updated the test to with your suggestion, seems to have done the trick. |
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)) \ |
There was a problem hiding this comment.
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
thanks! |
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 |
to_json
has a records type that turns a DataFrame into arecords json string. It'd be nice if you could do a similar thing but not have to go
json.loads
.