From ecfe8248a856594aaa3d10c758e1b724ba2e2024 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Thu, 22 Nov 2018 19:52:49 +0100 Subject: [PATCH] Second attempt at removing compat code from maybe_promote --- pandas/core/dtypes/cast.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index e08ae5d21c992..f6eb16bc9569d 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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