Skip to content

Commit e98e89f

Browse files
committed
fix odd tz hashing issue
1 parent b8d320a commit e98e89f

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

pandas/core/series.py

-2
Original file line numberDiff line numberDiff line change
@@ -2844,8 +2844,6 @@ def _sanitize_index(data, index, copy=False):
28442844
data = data.asobject
28452845
elif isinstance(data, DatetimeIndex):
28462846
data = data._to_embed(keep_tz=True)
2847-
if copy:
2848-
data = data.copy()
28492847
elif isinstance(data, np.ndarray):
28502848

28512849
# coerce datetimelike types

pandas/core/typed/dtypes.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,15 @@ def __new__(cls, unit=None, tz=None):
209209
raise ValueError("DatetimeTZDtype constructor must have a tz "
210210
"supplied")
211211

212+
# hash with the actual tz if we can
213+
# some cannot be hashed, so stringfy
214+
try:
215+
key = (unit, tz)
216+
hash(key)
217+
except TypeError:
218+
key = (unit, str(tz))
219+
212220
# set/retrieve from cache
213-
key = (unit, str(tz))
214221
try:
215222
return cls._cache[key]
216223
except KeyError:

pandas/tests/indexes/datetimes/test_construction.py

+15
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,36 @@
1212

1313
class TestDatetimeIndex(tm.TestCase):
1414

15+
def test_construction_caching(self):
16+
17+
df = pd.DataFrame({'dt': pd.date_range('20130101', periods=3),
18+
'dttz': pd.date_range('20130101', periods=3,
19+
tz='US/Eastern'),
20+
'dt_with_null': [pd.Timestamp('20130101'), pd.NaT,
21+
pd.Timestamp('20130103')],
22+
'dtns': pd.date_range('20130101', periods=3,
23+
freq='ns')})
24+
assert df.dttz.dtype.tz.zone == 'US/Eastern'
25+
1526
def test_construction_with_alt(self):
1627

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

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

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

2741
i2 = DatetimeIndex(
2842
i.tz_localize(None).asi8, dtype=i.dtype, tz=i.dtype.tz)
2943
self.assert_index_equal(i, i2)
44+
assert i.tz.zone == 'US/Eastern'
3045

3146
# localize into the provided tz
3247
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz='UTC')

pandas/tests/io/test_feather.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_basic(self):
5353
'dtns': pd.date_range('20130101', periods=3,
5454
freq='ns')})
5555

56-
assert df.dttz.dtype.tz.zone is not None
56+
assert df.dttz.dtype.tz.zone == 'US/Eastern'
5757
self.check_round_trip(df)
5858

5959
def test_strided_data_issues(self):

0 commit comments

Comments
 (0)