Skip to content

Commit

Permalink
Second attempt at removing compat code from maybe_promote
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Nov 22, 2018
1 parent 9bac669 commit ecfe824
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,29 +264,22 @@ def maybe_promote(dtype, fill_value=np.nan):

# returns tuple of (dtype, fill_value)
if issubclass(dtype.type, (np.datetime64, np.timedelta64)):
# for now: refuse to upcast datetime64
# (this is because datetime64 will not implicitly upconvert
# to object correctly as of numpy 1.6.1)
# TODO: remove old numpy compat code (without introducing segfault for
# tests/test_take.py::TestTake::test_2d_datetime64)
if isna(fill_value):
fill_value = iNaT
elif issubclass(dtype.type, np.datetime64):
try:
fill_value = tslibs.Timestamp(fill_value).value
except Exception:
dtype = np.object_
fill_value = np.nan
elif issubclass(dtype.type, np.timedelta64):
try:
fill_value = tslibs.Timedelta(fill_value).value
except Exception:
dtype = np.object_
fill_value = np.nan
else:
if issubclass(dtype.type, np.datetime64):
try:
fill_value = tslibs.Timestamp(fill_value).value
except Exception:
# the proper thing to do here would probably be to upcast
# to object (but numpy 1.6.1 doesn't do this properly)
fill_value = iNaT
elif issubclass(dtype.type, np.timedelta64):
try:
fill_value = tslibs.Timedelta(fill_value).value
except Exception:
# as for datetimes, cannot upcast to object
fill_value = iNaT
else:
fill_value = iNaT
fill_value = iNaT
elif is_datetimetz(dtype):
if isna(fill_value):
fill_value = iNaT
Expand Down

0 comments on commit ecfe824

Please sign in to comment.