Skip to content

Commit

Permalink
CLN: core/dtypes/cast.py::maybe_downcast_to_dtype (pandas-dev#37050)
Browse files Browse the repository at this point in the history
  • Loading branch information
arw2019 committed Oct 14, 2020
1 parent 0fa47b6 commit fad14fd
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Routines for casting.
"""

from contextlib import suppress
from datetime import date, datetime, timedelta
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type

Expand Down Expand Up @@ -156,12 +157,20 @@ def maybe_downcast_to_dtype(result, dtype):

dtype = np.dtype(dtype)

elif dtype.type is Period:
from pandas.core.arrays import PeriodArray

with suppress(TypeError):
# e.g. TypeError: int() argument must be a string, a
# bytes-like object or a number, not 'Period
return PeriodArray(result, freq=dtype.freq)

converted = maybe_downcast_numeric(result, dtype, do_round)
if converted is not result:
return converted

# a datetimelike
# GH12821, iNaT is casted to float
# GH12821, iNaT is cast to float
if dtype.kind in ["M", "m"] and result.dtype.kind in ["i", "f"]:
if hasattr(dtype, "tz"):
# not a numpy dtype
Expand All @@ -174,17 +183,6 @@ def maybe_downcast_to_dtype(result, dtype):
else:
result = result.astype(dtype)

elif dtype.type is Period:
# TODO(DatetimeArray): merge with previous elif
from pandas.core.arrays import PeriodArray

try:
return PeriodArray(result, freq=dtype.freq)
except TypeError:
# e.g. TypeError: int() argument must be a string, a
# bytes-like object or a number, not 'Period
pass

return result


Expand Down

0 comments on commit fad14fd

Please sign in to comment.