Skip to content

Commit 05d0667

Browse files
RobinFiveWordsjreback
RobinFiveWords
authored andcommitted
BUG: reshape fix for maybe_infer_to_datetimelike()
closes pandas-dev#16362 Author: RobinFiveWords <[email protected]> Closes pandas-dev#16395 from RobinFiveWords/cast-infer-datetime-reshape-fix and squashes the following commits: 7ad1e7d [RobinFiveWords] redid lost changes to cast.py and test_cast.py afa2eeb [RobinFiveWords] added whatsnew0.20.2 entry 7a35624 [RobinFiveWords] removed whatsnew entry again 2ec60a6 [RobinFiveWords] added back whatsnew change
1 parent 85080aa commit 05d0667

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Reshaping
8686
- Bug in ``DataFrame.stack`` with unsorted levels in MultiIndex columns (:issue:`16323`)
8787
- Bug in ``pd.wide_to_long()`` where no error was raised when ``i`` was not a unique identifier (:issue:`16382`)
8888
- Bug in ``Series.isin(..)`` with a list of tuples (:issue:`16394`)
89+
- Bug in construction of a ``DataFrame`` with mixed dtypes including an all-NaT column. (:issue:`16395`)
8990

9091

9192
Numeric

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def try_timedelta(v):
837837
try:
838838
return to_timedelta(v)._values.reshape(shape)
839839
except:
840-
return v
840+
return v.reshape(shape)
841841

842842
inferred_type = lib.infer_datetimelike_array(_ensure_object(v))
843843

pandas/tests/dtypes/test_cast.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from datetime import datetime, timedelta, date
1010
import numpy as np
1111

12-
from pandas import Timedelta, Timestamp, DatetimeIndex
12+
from pandas import Timedelta, Timestamp, DatetimeIndex, DataFrame, NaT
1313

1414
from pandas.core.dtypes.cast import (
1515
maybe_downcast_to_dtype,
@@ -213,6 +213,17 @@ def test_maybe_convert_scalar(self):
213213
result = maybe_convert_scalar(Timedelta('1 day 1 min'))
214214
assert result == Timedelta('1 day 1 min').value
215215

216+
def test_maybe_infer_to_datetimelike(self):
217+
# GH16362
218+
# pandas=0.20.1 raises IndexError: tuple index out of range
219+
result = DataFrame(np.array([[NaT, 'a', 'b', 0],
220+
[NaT, 'b', 'c', 1]]))
221+
assert result.size == 8
222+
# this construction was fine
223+
result = DataFrame(np.array([[NaT, 'a', 0],
224+
[NaT, 'b', 1]]))
225+
assert result.size == 6
226+
216227

217228
class TestConvert(object):
218229

0 commit comments

Comments
 (0)