-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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 45 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 |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ | |
| is_datetime64_dtype, | ||
| is_datetime64_ns_dtype, | ||
| is_datetime64tz_dtype, | ||
| is_datetime_or_timedelta_dtype, | ||
| is_dtype_equal, | ||
| is_extension_array_dtype, | ||
| is_float, | ||
|
|
@@ -154,6 +155,30 @@ def maybe_box_datetimelike(value: Scalar, dtype: Optional[Dtype] = None) -> Scal | |
| return value | ||
|
|
||
|
|
||
| def maybe_box_native(value: Scalar) -> Scalar: | ||
| """ | ||
| If passed a scalar cast the scalar to a python native type. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| value : scalar or Series | ||
|
|
||
| Returns | ||
| ------- | ||
| scalar or Series | ||
| """ | ||
| if is_datetime_or_timedelta_dtype(value): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| value = maybe_box_datetimelike(value) | ||
| elif is_float_dtype(value): | ||
| value = float(value) | ||
| elif is_integer_dtype(value): | ||
jreback marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| with suppress(ValueError, TypeError): | ||
|
||
| value = int(value) | ||
| elif is_bool_dtype(value): | ||
|
||
| value = bool(value) | ||
| return value | ||
|
|
||
|
|
||
| def maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: | ||
| """ | ||
| Convert a Timedelta or Timestamp to timedelta64 or datetime64 for setting | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from datetime import datetime | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pandas.core.dtypes.cast import maybe_box_native | ||
|
|
||
| from pandas import Timedelta, Timestamp | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "obj,expected_dtype", | ||
| [ | ||
| (int(4), int), | ||
| (np.uint(4), int), | ||
| (np.int32(-4), int), | ||
| (np.uint8(4), int), | ||
| (float(454.98), float), | ||
| (np.float16(0.4), float), | ||
| (np.float64(1.4), float), | ||
| (np.bool_(False), bool), | ||
| (datetime(2005, 2, 25), datetime), | ||
| (np.datetime64("2005-02-25"), Timestamp), | ||
| (Timestamp("2005-02-25"), Timestamp), | ||
| (np.timedelta64(1, "D"), Timedelta), | ||
| (Timedelta(1, "D"), Timedelta), | ||
| ], | ||
|
Contributor
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 add other types which don't have an analog (e.g. Period / Interval, bytes) and so return themselves.
Contributor
Author
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. added |
||
| ) | ||
| def test_maybe_box_native(obj, expected_dtype): | ||
| boxed_obj = maybe_box_native(obj) | ||
| result_dtype = type(boxed_obj) | ||
| assert result_dtype is expected_dtype | ||
Uh oh!
There was an error while loading. Please reload this page.