Skip to content

BUG: Bug in .loc partial setting with a np.datetime64 (GH9516) #9522

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

Merged
merged 1 commit into from
Feb 19, 2015
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down