Skip to content

Commit f4278a6

Browse files
Backport PR #42486: REGR: to_dict(orient={"records", "split", "dict"}) slowdown (#42502)
Co-authored-by: Matthew Zeitlin <[email protected]>
1 parent bac0684 commit f4278a6

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

asv_bench/benchmarks/frame_methods.py

+16
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ def time_to_html_mixed(self):
232232
self.df2.to_html()
233233

234234

235+
class ToDict:
236+
params = [["dict", "list", "series", "split", "records", "index"]]
237+
param_names = ["orient"]
238+
239+
def setup(self, orient):
240+
data = np.random.randint(0, 1000, size=(10000, 4))
241+
self.int_df = DataFrame(data)
242+
self.datetimelike_df = self.int_df.astype("timedelta64[ns]")
243+
244+
def time_to_dict_ints(self, orient):
245+
self.int_df.to_dict(orient=orient)
246+
247+
def time_to_dict_datetimelike(self, orient):
248+
self.datetimelike_df.to_dict(orient=orient)
249+
250+
235251
class ToNumpy:
236252
def setup(self):
237253
N = 10000

doc/source/whatsnew/v1.3.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- :class:`DataFrame` constructed with with an older version of pandas could not be unpickled (:issue:`42345`)
1919
- Performance regression in constructing a :class:`DataFrame` from a dictionary of dictionaries (:issue:`42338`)
2020
- Fixed regression in :meth:`DataFrame.agg` dropping values when the DataFrame had an Extension Array dtype, a duplicate index, and ``axis=1`` (:issue:`42380`)
21+
- Performance regression in :meth:`DataFrame.to_dict` and :meth:`Series.to_dict` when ``orient`` argument one of "records", "dict", or "split" (:issue:`42352`)
2122
- Fixed regression in indexing with a ``list`` subclass incorrectly raising ``TypeError`` (:issue:`42433`, :issue:42461`)
2223
-
2324

pandas/core/dtypes/cast.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
is_complex_dtype,
5959
is_datetime64_dtype,
6060
is_datetime64tz_dtype,
61-
is_datetime_or_timedelta_dtype,
6261
is_dtype_equal,
6362
is_extension_array_dtype,
6463
is_float,
@@ -182,9 +181,7 @@ def maybe_box_native(value: Scalar) -> Scalar:
182181
-------
183182
scalar or Series
184183
"""
185-
if is_datetime_or_timedelta_dtype(value):
186-
value = maybe_box_datetimelike(value)
187-
elif is_float(value):
184+
if is_float(value):
188185
# error: Argument 1 to "float" has incompatible type
189186
# "Union[Union[str, int, float, bool], Union[Any, Timestamp, Timedelta, Any]]";
190187
# expected "Union[SupportsFloat, _SupportsIndex, str]"
@@ -196,6 +193,8 @@ def maybe_box_native(value: Scalar) -> Scalar:
196193
value = int(value) # type: ignore[arg-type]
197194
elif is_bool(value):
198195
value = bool(value)
196+
elif isinstance(value, (np.datetime64, np.timedelta64)):
197+
value = maybe_box_datetimelike(value)
199198
return value
200199

201200

0 commit comments

Comments
 (0)