Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

MridulS
Copy link

@MridulS MridulS commented Dec 1, 2017

Add a new private function _tz_compare which compares timezones of DatetimeIndex.

@jreback

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a release not in bug fixes for 0.22.0

@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add for all the other cases of str(self.tz) != str(other) (or variants on this)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other places where pandas uses variants for str(self.tz) != str(other.tz) isn't particularly for DatetimeIndex. For example in test_timezones.py this is used to test the equality of Timestamp I guess we have to write a more general method which takes in any object with a .tz attribute and compare it. In that case which file should I put this method in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the right place to put this is in
pandas/_libs/tslibs/timezones.pyx

and just call it from where needed

Copy link
Member

Choose a reason for hiding this comment

The 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 master:

elif (isinstance(left, ABCDatetimeIndex) and
str(left.tz) != str(right.tz)):

@@ -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',
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to pandas/tests/reshape/test_concat.py

df3 = pd.DataFrame({'b': [1, 2, 3]}, index=idx3)
res = pd.concat([df1, df3], axis=1)

assert str(res.index.tzinfo) == 'UTC'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this, can you construct the expected frame directly and use
tm.assert_frame_equal (same as above as well)

@@ -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.
Copy link
Contributor

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.

@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Timezones Timezone data dtype labels Dec 1, 2017
@codecov
Copy link

codecov bot commented Dec 1, 2017

Codecov Report

Merging #18596 into master will decrease coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18596      +/-   ##
==========================================
- Coverage   91.46%   91.41%   -0.05%     
==========================================
  Files         157      157              
  Lines       51452    51449       -3     
==========================================
- Hits        47059    47033      -26     
- Misses       4393     4416      +23
Flag Coverage Δ
#multiple 89.28% <100%> (-0.03%) ⬇️
#single 40.58% <100%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.6% <100%> (-0.11%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/plotting/_converter.py 63.44% <0%> (-1.82%) ⬇️
pandas/core/indexes/multi.py 96.27% <0%> (-0.17%) ⬇️
pandas/core/frame.py 97.81% <0%> (-0.1%) ⬇️
pandas/core/base.py 96.54% <0%> (-0.01%) ⬇️
pandas/core/indexes/base.py 96.46% <0%> (-0.01%) ⬇️
pandas/core/series.py 94.8% <0%> (+0.04%) ⬆️
pandas/core/indexes/datetimelike.py 97.11% <0%> (+0.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d5ffb1f...259d6d8. Read the comment docs.

@codecov
Copy link

codecov bot commented Dec 1, 2017

Codecov Report

Merging #18596 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18596      +/-   ##
==========================================
- Coverage   91.46%   91.44%   -0.02%     
==========================================
  Files         157      157              
  Lines       51452    51449       -3     
==========================================
- Hits        47059    47048      -11     
- Misses       4393     4401       +8
Flag Coverage Δ
#multiple 89.31% <100%> (ø) ⬆️
#single 40.58% <100%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.7% <100%> (-0.02%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/indexes/multi.py 96.27% <0%> (-0.17%) ⬇️
pandas/core/frame.py 97.81% <0%> (-0.1%) ⬇️
pandas/core/base.py 96.54% <0%> (-0.01%) ⬇️
pandas/core/indexes/base.py 96.46% <0%> (-0.01%) ⬇️
pandas/core/series.py 94.8% <0%> (+0.04%) ⬆️
pandas/util/testing.py 81.89% <0%> (+0.19%) ⬆️
pandas/core/indexes/datetimelike.py 97.11% <0%> (+0.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d5ffb1f...259d6d8. Read the comment docs.

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could/should this be part of DatetimeIbdex._has_same_tz?

@jreback
Copy link
Contributor

jreback commented Dec 2, 2017

Could/should this be part of DatetimeIbdex._has_same_tz?

so looks there was an attempt to do this with _has_same_tz
can leave DatetimeIndex._has_same_tz as this has a conversion for a scalar as input. But let's remove _timezone, and replace _has_same_tz this call e.g. something like

    def _has_same_tz(self, other):

        if isinstance(other, (np.datetime64, date, datetime)):
            other = Timestamp(other)
        return timezones.tz_compare(self.tz, other.tz)

in tslibs/timezones.

cpdef tz_compare(object left, object, right):
    left = getattr(left, 'tz', '_no_tz')
    right = getattr(right, 'tz', '_no_tz')

     return get_timezone(left) == get_timezone(right)

and in most of the calls that right now are str(self.tz) == str(other.tz) can use _has_same_tz (maybe have to use tz_compare in some)

@jreback
Copy link
Contributor

jreback commented Dec 8, 2017

xref #18558

cc @jschendel

@jreback
Copy link
Contributor

jreback commented Jan 17, 2018

superseded by #19281

@jreback jreback closed this Feb 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pd.concat reset the tz-aware index to UTC
4 participants