Skip to content

BUG: reshape fix for maybe_infer_to_datetimelike() #16395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/dtypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):

Expand Down