Skip to content

Commit b41ea09

Browse files
authored
BUG: Fix to_dict with datelike types and orient=list (#57157)
1 parent 9b95e45 commit b41ea09

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
1919
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
2020
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
21+
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
2122
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
2223
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
2324
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)

pandas/core/methods/to_dict.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,9 @@ def to_dict(
171171
return into_c(
172172
(
173173
k,
174-
list(
175-
map(
176-
maybe_box_native, v.to_numpy(na_value=box_na_values[i]).tolist()
177-
)
178-
)
174+
list(map(maybe_box_native, v.to_numpy(na_value=box_na_values[i])))
179175
if i in object_dtype_indices_as_set
180-
else v.to_numpy().tolist(),
176+
else list(map(maybe_box_native, v.to_numpy())),
181177
)
182178
for i, (k, v) in enumerate(df.items())
183179
)

pandas/tests/frame/methods/test_to_dict.py

+14
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
NA,
1313
DataFrame,
1414
Index,
15+
Interval,
1516
MultiIndex,
17+
Period,
1618
Series,
19+
Timedelta,
1720
Timestamp,
1821
)
1922
import pandas._testing as tm
@@ -519,3 +522,14 @@ def test_to_dict_pos_args_deprecation(self):
519522
)
520523
with tm.assert_produces_warning(FutureWarning, match=msg):
521524
df.to_dict("records", {})
525+
526+
527+
@pytest.mark.parametrize(
528+
"val", [Timestamp(2020, 1, 1), Timedelta(1), Period("2020"), Interval(1, 2)]
529+
)
530+
def test_to_dict_list_pd_scalars(val):
531+
# GH 54824
532+
df = DataFrame({"a": [val]})
533+
result = df.to_dict(orient="list")
534+
expected = {"a": [val]}
535+
assert result == expected

0 commit comments

Comments
 (0)