Skip to content

Commit 757233e

Browse files
committed
enact TODO: relocate PeriodArray block
1 parent dc8dcdc commit 757233e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

pandas/core/dtypes/cast.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def maybe_downcast_to_dtype(result, dtype: Dtype):
147147
elif isinstance(result, ABCDataFrame):
148148
# occurs in pivot_table doctest
149149
return result
150+
elif is_period_dtype(dtype):
151+
from pandas.core.arrays import PeriodArray
152+
153+
with suppress(TypeError):
154+
# e.g. TypeError: int() argument must be a string, a
155+
# bytes-like object or a number, not 'Period
156+
return PeriodArray(result, freq=dtype.freq)
150157

151158
if isinstance(dtype, str):
152159
if dtype == "infer":
@@ -178,25 +185,17 @@ def maybe_downcast_to_dtype(result, dtype: Dtype):
178185
# a datetimelike
179186
# GH12821, iNaT is cast to float
180187
if is_datetime_or_timedelta_any_dtype(dtype) and result.dtype.kind in ["i", "f"]:
181-
if not is_datetime_or_timedelta_dtype(dtype):
188+
189+
if is_datetime_or_timedelta_dtype(dtype):
190+
result = result.astype(dtype)
191+
else:
182192
# not a numpy dtype
183193
if dtype.tz:
184194
# convert to datetime and change timezone
185195
from pandas import to_datetime
186196

187197
result = to_datetime(result).tz_localize("utc")
188198
result = result.tz_convert(dtype.tz)
189-
else:
190-
result = result.astype(dtype)
191-
192-
elif is_period_dtype(dtype):
193-
# TODO(DatetimeArray): merge with previous elif
194-
from pandas.core.arrays import PeriodArray
195-
196-
with suppress(TypeError):
197-
# e.g. TypeError: int() argument must be a string, a
198-
# bytes-like object or a number, not 'Period
199-
return PeriodArray(result, freq=dtype.freq)
200199

201200
return result
202201

0 commit comments

Comments
 (0)