-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Series.to_dict does not return native Python types #37648
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
Changes from 9 commits
a2e3e55
41f0a4b
5e4edbe
b6967b7
55919e0
759e091
6f966f2
1e058ab
2ebd673
1dc5935
1e5e459
3c6bd7e
e3cc18f
22819b7
fb782df
cb389d9
249968e
1f5d442
9686035
732fb84
ec6cbfc
4bb1916
587e592
9d81f54
673da4e
ef639c9
4036b63
89a841d
68420ea
8b83e24
86b0e04
efc95b8
c3b723a
d5a9476
3e2ea12
a455fcc
5541a35
fcbf705
9282ca3
a444ef5
2411a70
99d7c55
e009a9e
eaeb409
a642a6b
0467e1b
5605bbd
39ca508
7fa7503
08567b4
7c47df2
618f8ef
da620f2
761b728
49acd25
86c6aa7
4650131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,31 +256,42 @@ def test_to_dict_wide(self): | |
expected = {f"A_{i:d}": i for i in range(256)} | ||
assert result == expected | ||
|
||
def test_to_dict_orient_dtype(self): | ||
# GH22620 & GH21256 | ||
|
||
df = DataFrame( | ||
{ | ||
"bool": [True, True, False], | ||
"datetime": [ | ||
@pytest.mark.parametrize( | ||
"data,dtype", | ||
( | ||
([True, True, False], bool), | ||
[ | ||
[ | ||
datetime(2018, 1, 1), | ||
datetime(2019, 2, 2), | ||
datetime(2020, 3, 3), | ||
], | ||
"float": [1.0, 2.0, 3.0], | ||
"int": [1, 2, 3], | ||
"str": ["X", "Y", "Z"], | ||
} | ||
) | ||
Timestamp, | ||
], | ||
[[1.0, 2.0, 3.0], float], | ||
[[1, 2, 3], int], | ||
[["X", "Y", "Z"], str], | ||
), | ||
) | ||
def test_to_dict_orient_dtype(self, data, dtype): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# GH22620 & GH21256 | ||
|
||
expected = { | ||
"int": int, | ||
"float": float, | ||
"str": str, | ||
"datetime": Timestamp, | ||
"bool": bool, | ||
} | ||
df = DataFrame({"a": data}) | ||
d = df.to_dict(orient="records") | ||
assert all(type(record["a"]) is dtype for record in d) | ||
|
||
@pytest.mark.parametrize( | ||
"data,dtype", | ||
( | ||
[np.int64(9), int], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add unsigned int as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
[np.float64(1.1), float], | ||
[np.bool_(True), bool], | ||
[np.datetime64("2005-02-25"), Timestamp], | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
) | ||
def test_to_dict_scalar_constructor_orient_dtype(self, data, dtype): | ||
# GH22620 & GH21256 | ||
|
||
for df_dict in df.to_dict("records"): | ||
result = {col: type(df_dict[col]) for col in list(df.columns)} | ||
assert result == expected | ||
df = DataFrame({"a": data}, index=[0]) | ||
d = df.to_dict(orient="records") | ||
assert type(d[0]["a"]) is dtype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you break this out a bit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
Uh oh!
There was an error while loading. Please reload this page.