Skip to content

BUG: getitem and a series with a non-ndarray values #12151

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

Closed
wants to merge 1 commit into from
Closed
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.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ Bug Fixes
- Compat for numpy 1.11 w.r.t. ``NaT`` comparison changes (:issue:`12049`)
- Bug in ``read_csv`` when reading from a ``StringIO`` in threads (:issue:`11790`)
- Bug in not treating ``NaT`` as a missing value in datetimelikes when factorizing & with ``Categoricals`` (:issue:`12077`)

- Bug in getitem when the values of a ``Series`` were tz-aware (:issue:`12089`)



Expand Down
4 changes: 2 additions & 2 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cdef class IndexEngine:
hash(val)
return val in self.mapping

cpdef get_value(self, ndarray arr, object key):
cpdef get_value(self, ndarray arr, object key, object tz=None):
'''
arr : 1-dimensional ndarray
'''
Expand All @@ -113,7 +113,7 @@ cdef class IndexEngine:
return arr[loc]
else:
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_at(arr, loc))
return Timestamp(util.get_value_at(arr, loc), tz=tz)
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_at(arr, loc))
return util.get_value_at(arr, loc)
Expand Down
10 changes: 8 additions & 2 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,12 @@ def get_value(self, series, key):
# use this, e.g. DatetimeIndex
s = getattr(series, '_values', None)
if isinstance(s, Index) and lib.isscalar(key):
return s[key]
try:
return s[key]
except (IndexError, ValueError):

# invalid type as an indexer
pass

s = _values_from_object(series)
k = _values_from_object(key)
Expand All @@ -1891,7 +1896,8 @@ def get_value(self, series, key):
raise KeyError

try:
return self._engine.get_value(s, k)
return self._engine.get_value(s, k,
tz=getattr(series.dtype, 'tz', None))
except KeyError as e1:
if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
raise
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/series/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,18 @@ def test_basic_getitem_with_labels(self):
expected = s.reindex(arr_inds)
assert_series_equal(result, expected)

# GH12089
# with tz for values
s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"),
index=['a', 'b', 'c'])
expected = Timestamp('2011-01-01', tz='US/Eastern')
result = s.loc['a']
self.assertEqual(result, expected)
result = s.iloc[0]
self.assertEqual(result, expected)
result = s['a']
self.assertEqual(result, expected)

def test_basic_setitem_with_labels(self):
indices = self.ts.index[[5, 10, 15]]

Expand Down Expand Up @@ -650,6 +662,26 @@ def test_basic_setitem_with_labels(self):
self.assertRaises(Exception, s.__setitem__, inds_notfound, 0)
self.assertRaises(Exception, s.__setitem__, arr_inds_notfound, 0)

# GH12089
# with tz for values
s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"),
index=['a', 'b', 'c'])
s2 = s.copy()
expected = Timestamp('2011-01-03', tz='US/Eastern')
s2.loc['a'] = expected
result = s2.loc['a']
self.assertEqual(result, expected)

s2 = s.copy()
s2.iloc[0] = expected
result = s2.iloc[0]
self.assertEqual(result, expected)

s2 = s.copy()
s2['a'] = expected
result = s2['a']
self.assertEqual(result, expected)

def test_ix_getitem(self):
inds = self.series.index[[3, 4, 7]]
assert_series_equal(self.series.ix[inds], self.series.reindex(inds))
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,8 @@ def get_value_maybe_box(self, series, key):
key = Timestamp(key, tz=self.tz)
elif not isinstance(key, Timestamp):
key = Timestamp(key)
values = self._engine.get_value(_values_from_object(series), key)
values = self._engine.get_value(_values_from_object(series),
key, tz=self.tz)
return _maybe_box(self, values, series, key)

def get_loc(self, key, method=None, tolerance=None):
Expand Down