Skip to content

Commit 4671aeb

Browse files
committed
Recognize timezoned labels when accessing dataframes.
1 parent e457325 commit 4671aeb

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,52 +1273,57 @@ def _parsed_string_to_bounds(self, reso, parsed):
12731273
lower, upper: pd.Timestamp
12741274
12751275
"""
1276+
if parsed.tzinfo is None:
1277+
target_tz = self.tz
1278+
else:
1279+
target_tz = parsed.tzinfo
1280+
12761281
if reso == 'year':
1277-
return (Timestamp(datetime(parsed.year, 1, 1), tz=self.tz),
1282+
return (Timestamp(datetime(parsed.year, 1, 1), tz=target_tz),
12781283
Timestamp(datetime(parsed.year, 12, 31, 23,
1279-
59, 59, 999999), tz=self.tz))
1284+
59, 59, 999999), tz=target_tz))
12801285
elif reso == 'month':
12811286
d = libts.monthrange(parsed.year, parsed.month)[1]
12821287
return (Timestamp(datetime(parsed.year, parsed.month, 1),
1283-
tz=self.tz),
1288+
tz=target_tz),
12841289
Timestamp(datetime(parsed.year, parsed.month, d, 23,
1285-
59, 59, 999999), tz=self.tz))
1290+
59, 59, 999999), target_tz))
12861291
elif reso == 'quarter':
12871292
qe = (((parsed.month - 1) + 2) % 12) + 1 # two months ahead
12881293
d = libts.monthrange(parsed.year, qe)[1] # at end of month
12891294
return (Timestamp(datetime(parsed.year, parsed.month, 1),
1290-
tz=self.tz),
1295+
tz=target_tz),
12911296
Timestamp(datetime(parsed.year, qe, d, 23, 59,
1292-
59, 999999), tz=self.tz))
1297+
59, 999999), tz=target_tz))
12931298
elif reso == 'day':
12941299
st = datetime(parsed.year, parsed.month, parsed.day)
1295-
return (Timestamp(st, tz=self.tz),
1300+
return (Timestamp(st, tz=target_tz),
12961301
Timestamp(Timestamp(st + offsets.Day(),
1297-
tz=self.tz).value - 1))
1302+
tz=target_tz).value - 1))
12981303
elif reso == 'hour':
12991304
st = datetime(parsed.year, parsed.month, parsed.day,
13001305
hour=parsed.hour)
1301-
return (Timestamp(st, tz=self.tz),
1306+
return (Timestamp(st, tz=target_tz),
13021307
Timestamp(Timestamp(st + offsets.Hour(),
1303-
tz=self.tz).value - 1))
1308+
tz=target_tz).value - 1))
13041309
elif reso == 'minute':
13051310
st = datetime(parsed.year, parsed.month, parsed.day,
13061311
hour=parsed.hour, minute=parsed.minute)
1307-
return (Timestamp(st, tz=self.tz),
1312+
return (Timestamp(st, tz=target_tz),
13081313
Timestamp(Timestamp(st + offsets.Minute(),
1309-
tz=self.tz).value - 1))
1314+
tz=target_tz).value - 1))
13101315
elif reso == 'second':
13111316
st = datetime(parsed.year, parsed.month, parsed.day,
13121317
hour=parsed.hour, minute=parsed.minute,
13131318
second=parsed.second)
1314-
return (Timestamp(st, tz=self.tz),
1319+
return (Timestamp(st, tz=target_tz),
13151320
Timestamp(Timestamp(st + offsets.Second(),
1316-
tz=self.tz).value - 1))
1321+
tz=target_tz).value - 1))
13171322
elif reso == 'microsecond':
13181323
st = datetime(parsed.year, parsed.month, parsed.day,
13191324
parsed.hour, parsed.minute, parsed.second,
13201325
parsed.microsecond)
1321-
return (Timestamp(st, tz=self.tz), Timestamp(st, tz=self.tz))
1326+
return (Timestamp(st, tz=target_tz), Timestamp(st, tz=target_tz))
13221327
else:
13231328
raise KeyError
13241329

pandas/tests/indexing/test_datetime.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ def test_consistency_with_tz_aware_scalar(self):
123123
result = df[0].at[0]
124124
assert result == expected
125125

126+
def test_access_datetimeindex_with_timezoned_label(self):
127+
128+
idx = pd.DataFrame(index=pd.date_range('2016-01-01T00:00', '2016-03-31T23:59', freq='T'))
129+
130+
former_naive_endpoint_idx = idx[
131+
"2016-01-01T00:00-02:00"
132+
:
133+
"2016-01-01T02:03"
134+
]
135+
136+
former_non_naive_endpoint_idx = idx[
137+
pd.Timestamp("2016-01-01T00:00-02:00")
138+
:
139+
pd.Timestamp("2016-01-01T02:03")
140+
]
141+
142+
assert len(former_naive_endpoint_idx) == len(former_non_naive_endpoint_idx)
143+
144+
assert former_naive_endpoint_idx.iloc[0].name == former_non_naive_endpoint_idx.iloc[0].name
145+
assert former_naive_endpoint_idx.iloc[1].name == former_non_naive_endpoint_idx.iloc[1].name
146+
assert former_naive_endpoint_idx.iloc[2].name == former_non_naive_endpoint_idx.iloc[2].name
147+
assert former_naive_endpoint_idx.iloc[3].name == former_non_naive_endpoint_idx.iloc[3].name
148+
149+
126150
def test_indexing_with_datetimeindex_tz(self):
127151

128152
# GH 12050

0 commit comments

Comments
 (0)