diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index 0468c220bcb98..52d1b7b537d5a 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -227,7 +227,7 @@ Bug Fixes column with timezone info) to the according sqlalchemy type (:issue:`9085`). - Fixed bug in ``to_sql`` ``dtype`` argument not accepting an instantiated SQLAlchemy type (:issue:`9083`). - +- Bug in ``.loc`` partial setting with a ``np.datetime64`` (:issue:`9516`) - 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. diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 426fce0797ad2..1c951f58a17d8 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -246,6 +246,7 @@ def _setitem_with_indexer(self, indexer, value): new_indexer = convert_from_missing_indexer_tuple( indexer, self.obj.axes) self._setitem_with_indexer(new_indexer, value) + return self.obj # reindex the axis diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 424e5009f99c0..68c504b2a35c3 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -3171,6 +3171,22 @@ def test_partial_setting_with_datetimelike_dtype(self): df.loc[mask, 'C'] = df.loc[mask].index assert_frame_equal(df, expected) + def test_loc_setitem_datetime(self): + + # GH 9516 + dt1 = Timestamp('20130101 09:00:00') + dt2 = Timestamp('20130101 10:00:00') + + for conv in [lambda x: x, lambda x: x.to_datetime64(), + lambda x: x.to_pydatetime(), lambda x: np.datetime64(x)]: + + df = pd.DataFrame() + df.loc[conv(dt1),'one'] = 100 + df.loc[conv(dt2),'one'] = 200 + + expected = DataFrame({'one' : [100.0,200.0]},index=[dt1,dt2]) + assert_frame_equal(df, expected) + def test_series_partial_set(self): # partial set with new index # Regression from GH4825 diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index 2205c6c4f4a64..da1f1dce435b8 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1509,7 +1509,7 @@ def insert(self, loc, item): """ freq = None - if isinstance(item, datetime): + if isinstance(item, (datetime, np.datetime64)): zone = tslib.get_timezone(self.tz) izone = tslib.get_timezone(getattr(item, 'tzinfo', None)) if zone != izone: