diff --git a/doc/source/whatsnew/v0.20.2.txt b/doc/source/whatsnew/v0.20.2.txt index 6d6a148ed025f..b327e69348755 100644 --- a/doc/source/whatsnew/v0.20.2.txt +++ b/doc/source/whatsnew/v0.20.2.txt @@ -84,6 +84,7 @@ Reshaping - Bug in ``DataFrame.stack`` with unsorted levels in MultiIndex columns (:issue:`16323`) - Bug in ``pd.wide_to_long()`` where no error was raised when ``i`` was not a unique identifier (:issue:`16382`) - Bug in ``Series.isin(..)`` with a list of tuples (:issue:`16394`) +- Bug in ``maybe_infer_to_datetimelike()`` in ``core.dtypes.cast`` where result was not reshaped in all cases (:issue:`16395`) Numeric diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 19d3792f73de7..fd61813a57c98 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -837,7 +837,7 @@ def try_timedelta(v): try: return to_timedelta(v)._values.reshape(shape) except: - return v + return v.reshape(shape) inferred_type = lib.infer_datetimelike_array(_ensure_object(v)) diff --git a/pandas/tests/dtypes/test_cast.py b/pandas/tests/dtypes/test_cast.py index e92724a5d9cd4..767e99d98cf29 100644 --- a/pandas/tests/dtypes/test_cast.py +++ b/pandas/tests/dtypes/test_cast.py @@ -9,7 +9,7 @@ from datetime import datetime, timedelta, date import numpy as np -from pandas import Timedelta, Timestamp, DatetimeIndex +from pandas import Timedelta, Timestamp, DatetimeIndex, DataFrame, NaT from pandas.core.dtypes.cast import ( maybe_downcast_to_dtype, @@ -213,6 +213,17 @@ def test_maybe_convert_scalar(self): result = maybe_convert_scalar(Timedelta('1 day 1 min')) assert result == Timedelta('1 day 1 min').value + def test_maybe_infer_to_datetimelike(self): + # GH16362 + # pandas=0.20.1 raises IndexError: tuple index out of range + result = DataFrame(np.array([[NaT, 'a', 'b', 0], + [NaT, 'b', 'c', 1]])) + assert result.size == 8 + # this construction was fine + result = DataFrame(np.array([[NaT, 'a', 0], + [NaT, 'b', 1]])) + assert result.size == 6 + class TestConvert(object):