Skip to content

Commit 0b74c72

Browse files
committed
Merge pull request pandas-dev#10389 from behzadnouri/nat-reset-index
BUG: closes bug in reset_index when index contains NaT
2 parents d8a2f30 + c6d7a9a commit 0b74c72

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ Bug Fixes
6060
~~~~~~~~~
6161
- Bug in ``DataFrame.apply`` when function returns categorical series. (:issue:`9573`)
6262
- Bug in ``pd.Series`` when setting a value on an empty ``Series`` whose index has a frequency. (:issue:`10193`)
63+
- Bug in ``DataFrame.reset_index`` when index contains `NaT`. (:issue:`10388`)

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ def _maybe_upcast_putmask(result, mask, other):
11941194
if result.dtype in _DATELIKE_DTYPES:
11951195
if lib.isscalar(other):
11961196
if isnull(other):
1197-
other = tslib.iNaT
1197+
other = result.dtype.type('nat')
11981198
elif is_integer(other):
11991199
other = np.array(other, dtype=result.dtype)
12001200
elif is_integer_dtype(other):

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -9494,6 +9494,18 @@ def test_reindex_nan(self):
94949494
df.index = df.index.astype('object')
94959495
tm.assert_frame_equal(df.reindex(i), df.iloc[j])
94969496

9497+
# GH10388
9498+
df = pd.DataFrame({'other':['a', 'b', np.nan, 'c'],
9499+
'date':['2015-03-22', np.nan, '2012-01-08', np.nan],
9500+
'amount':[2, 3, 4, 5]})
9501+
9502+
df['date'] = pd.to_datetime(df.date)
9503+
df['delta'] = (pd.to_datetime('2015-06-18') - df['date']).shift(1)
9504+
9505+
left = df.set_index(['delta', 'other', 'date']).reset_index()
9506+
right = df.reindex(columns=['delta', 'other', 'date', 'amount'])
9507+
assert_frame_equal(left, right)
9508+
94979509
def test_reindex_name_remains(self):
94989510
s = Series(random.rand(10))
94999511
df = DataFrame(s, index=np.arange(len(s)))

0 commit comments

Comments
 (0)