-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Add test for GH 18523 and add _tz_compare() to compare timezone of DatetimeIndex #18596
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1127,6 +1127,21 @@ def join(self, other, how='left', level=None, return_indexers=False, | |||||
return Index.join(this, other, how=how, level=level, | ||||||
return_indexers=return_indexers, sort=sort) | ||||||
|
||||||
def _tz_compare(self, other): | ||||||
""" | ||||||
Compare time zones of DatetimeIndex. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
other: DatetimeIndex | ||||||
|
||||||
Returns: | ||||||
------- | ||||||
compare : Boolean | ||||||
|
||||||
""" | ||||||
return str(self.tzinfo) == str(other.tzinfo) | ||||||
|
||||||
def _maybe_utc_convert(self, other): | ||||||
this = self | ||||||
if isinstance(other, DatetimeIndex): | ||||||
|
@@ -1138,7 +1153,7 @@ def _maybe_utc_convert(self, other): | |||||
raise TypeError('Cannot join tz-naive with tz-aware ' | ||||||
'DatetimeIndex') | ||||||
|
||||||
if self.tz != other.tz: | ||||||
if not self._tz_compare(other): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also add for all the other cases of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other places where pandas uses variants for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the right place to put this is in and just call it from where needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI - I added an additional instance of this in a commit that went in earlier today. Current location on pandas/pandas/core/indexes/interval.py Lines 242 to 243 in 288bf6e
|
||||||
this = self.tz_convert('UTC') | ||||||
other = other.tz_convert('UTC') | ||||||
return this, other | ||||||
|
@@ -1243,7 +1258,7 @@ def __iter__(self): | |||||
|
||||||
def _wrap_union_result(self, other, result): | ||||||
name = self.name if self.name == other.name else None | ||||||
if self.tz != other.tz: | ||||||
if not self._tz_compare(other): | ||||||
raise ValueError('Passed item and index have different timezone') | ||||||
return self._simple_new(result, name=name, freq=None, tz=self.tz) | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -443,6 +443,26 @@ def test_000constructor_resolution(self): | |
|
||
assert idx.nanosecond[0] == t1.nanosecond | ||
|
||
def test_concat(self): | ||
idx1 = pd.date_range('2011-01-01', periods=3, freq='H', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls add the issue number as a comment |
||
tz='Europe/Paris') | ||
idx2 = pd.date_range(start=idx1[0], end=idx1[-1], freq='H') | ||
df1 = pd.DataFrame({'a': [1, 2, 3]}, index=idx1) | ||
df2 = pd.DataFrame({'b': [1, 2, 3]}, index=idx2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you move this to |
||
res = pd.concat([df1, df2], axis=1) | ||
|
||
assert str(res.index.tzinfo) == str(df1.index.tzinfo) | ||
assert str(res.index.tzinfo) == str(df2.index.tzinfo) | ||
|
||
idx3 = pd.date_range('2011-01-01', periods=3, | ||
freq='H', tz='Asia/Tokyo') | ||
df3 = pd.DataFrame({'b': [1, 2, 3]}, index=idx3) | ||
res = pd.concat([df1, df3], axis=1) | ||
|
||
assert str(res.index.tzinfo) == 'UTC' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of this, can you construct the expected frame directly and use |
||
assert str(res.index.tzinfo) != str(df1.index.tzinfo) | ||
assert str(res.index.tzinfo) != str(df3.index.tzinfo) | ||
|
||
|
||
class TestTimeSeries(object): | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you expound on the reason for this here (e.g. mention the directly comparing
pytz
of the same timezone is broken), but a str comparison is ok.