Skip to content

Commit

Permalink
fix odd tz hashing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Apr 14, 2017
1 parent b8d320a commit e98e89f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2844,8 +2844,6 @@ def _sanitize_index(data, index, copy=False):
data = data.asobject
elif isinstance(data, DatetimeIndex):
data = data._to_embed(keep_tz=True)
if copy:
data = data.copy()
elif isinstance(data, np.ndarray):

# coerce datetimelike types
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/typed/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,15 @@ def __new__(cls, unit=None, tz=None):
raise ValueError("DatetimeTZDtype constructor must have a tz "
"supplied")

# hash with the actual tz if we can
# some cannot be hashed, so stringfy
try:
key = (unit, tz)
hash(key)
except TypeError:
key = (unit, str(tz))

# set/retrieve from cache
key = (unit, str(tz))
try:
return cls._cache[key]
except KeyError:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,36 @@

class TestDatetimeIndex(tm.TestCase):

def test_construction_caching(self):

df = pd.DataFrame({'dt': pd.date_range('20130101', periods=3),
'dttz': pd.date_range('20130101', periods=3,
tz='US/Eastern'),
'dt_with_null': [pd.Timestamp('20130101'), pd.NaT,
pd.Timestamp('20130103')],
'dtns': pd.date_range('20130101', periods=3,
freq='ns')})
assert df.dttz.dtype.tz.zone == 'US/Eastern'

def test_construction_with_alt(self):

i = pd.date_range('20130101', periods=5, freq='H', tz='US/Eastern')
i2 = DatetimeIndex(i, dtype=i.dtype)
self.assert_index_equal(i, i2)
assert i.tz.zone == 'US/Eastern'

i2 = DatetimeIndex(i.tz_localize(None).asi8, tz=i.dtype.tz)
self.assert_index_equal(i, i2)
assert i.tz.zone == 'US/Eastern'

i2 = DatetimeIndex(i.tz_localize(None).asi8, dtype=i.dtype)
self.assert_index_equal(i, i2)
assert i.tz.zone == 'US/Eastern'

i2 = DatetimeIndex(
i.tz_localize(None).asi8, dtype=i.dtype, tz=i.dtype.tz)
self.assert_index_equal(i, i2)
assert i.tz.zone == 'US/Eastern'

# localize into the provided tz
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz='UTC')
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_basic(self):
'dtns': pd.date_range('20130101', periods=3,
freq='ns')})

assert df.dttz.dtype.tz.zone is not None
assert df.dttz.dtype.tz.zone == 'US/Eastern'
self.check_round_trip(df)

def test_strided_data_issues(self):
Expand Down

0 comments on commit e98e89f

Please sign in to comment.