Skip to content

Commit a064c7d

Browse files
committed
Merge pull request #9522 from jreback/npdt
BUG: Bug in .loc partial setting with a np.datetime64 (GH9516)
2 parents bceb342 + d3d9f45 commit a064c7d

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Bug Fixes
227227
column with timezone info) to the according sqlalchemy type (:issue:`9085`).
228228
- Fixed bug in ``to_sql`` ``dtype`` argument not accepting an instantiated
229229
SQLAlchemy type (:issue:`9083`).
230-
230+
- Bug in ``.loc`` partial setting with a ``np.datetime64`` (:issue:`9516`)
231231

232232
- Items in ``Categorical.unique()`` (and ``s.unique()`` if ``s`` is of dtype ``category``) now appear in the order in which they are originally found, not in sorted order (:issue:`9331`). This is now consistent with the behavior for other dtypes in pandas.
233233

pandas/core/indexing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def _setitem_with_indexer(self, indexer, value):
246246
new_indexer = convert_from_missing_indexer_tuple(
247247
indexer, self.obj.axes)
248248
self._setitem_with_indexer(new_indexer, value)
249+
249250
return self.obj
250251

251252
# reindex the axis

pandas/tests/test_indexing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,22 @@ def test_partial_setting_with_datetimelike_dtype(self):
31713171
df.loc[mask, 'C'] = df.loc[mask].index
31723172
assert_frame_equal(df, expected)
31733173

3174+
def test_loc_setitem_datetime(self):
3175+
3176+
# GH 9516
3177+
dt1 = Timestamp('20130101 09:00:00')
3178+
dt2 = Timestamp('20130101 10:00:00')
3179+
3180+
for conv in [lambda x: x, lambda x: x.to_datetime64(),
3181+
lambda x: x.to_pydatetime(), lambda x: np.datetime64(x)]:
3182+
3183+
df = pd.DataFrame()
3184+
df.loc[conv(dt1),'one'] = 100
3185+
df.loc[conv(dt2),'one'] = 200
3186+
3187+
expected = DataFrame({'one' : [100.0,200.0]},index=[dt1,dt2])
3188+
assert_frame_equal(df, expected)
3189+
31743190
def test_series_partial_set(self):
31753191
# partial set with new index
31763192
# Regression from GH4825

pandas/tseries/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ def insert(self, loc, item):
15091509
"""
15101510

15111511
freq = None
1512-
if isinstance(item, datetime):
1512+
if isinstance(item, (datetime, np.datetime64)):
15131513
zone = tslib.get_timezone(self.tz)
15141514
izone = tslib.get_timezone(getattr(item, 'tzinfo', None))
15151515
if zone != izone:

0 commit comments

Comments
 (0)