Skip to content
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

TST: add datetimelike tests for tz-aware DatetimeIndex #17694

Closed
wants to merge 1 commit into from

Conversation

azjps
Copy link

@azjps azjps commented Sep 27, 2017

This commit just makes quick patches to get tests working for subclassing common index tests for tz-aware DatetimeIndex, by either ignoring tests entirely or explicitly converting to the DatetimeIndex's TZ. This is split off from PR #17583 where we were trying to add another common test. Advice is requested for whether to try to also make fixes upstream or just to keep these fixes localized to the test suite.

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

This commit just makes quick patches to get tests working
for subclassing common index tests for DatetimeIndex,
by either ignoring tests entirely or converting to the
correct TZ.
@azjps
Copy link
Author

azjps commented Sep 27, 2017

(Most of the CI builds are failing, but as far as I can tell they are miscellaneous errors unrelated to this PR.)

@sinhrks sinhrks added Datetime Datetime data dtype Testing pandas testing functions or related to the test suite labels Sep 27, 2017
@@ -34,6 +34,12 @@ def verify_pickle(self, index):
unpickled = tm.round_trip_pickle(index)
assert index.equals(unpickled)

def _fix_tz(self, new_index, orig_index):
if hasattr(orig_index, 'tz'):
Copy link
Contributor

Choose a reason for hiding this comment

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

really don't spell things like this. rather change create_index() to work properly.

def _fix_tz(self, new_index, orig_index):
if hasattr(orig_index, 'tz'):
assert new_index.tz is None
new_index = new_index.tz_localize('UTC').tz_convert(orig_index.tz)
Copy link
Contributor

Choose a reason for hiding this comment

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

create a new class DatetimeTZ in test_datetimelike.py which also inherits Datetimelike. Then just change its create_index

Copy link
Author

@azjps azjps Sep 28, 2017

Choose a reason for hiding this comment

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

To clarify, this commit already introduces a new Datetimelike-derived class in test_datetimelikely.py with create_index() overriden to return a tz-aware DatetimeIndex: test_datetimelike.py#L159. This _fix_tz hack here is not to create the underlying index to be tested, but to "fix" various transformations of the index which don't preserve TZ, to get them to compare correctly back to the output of create_index(). But like you mentioned in the comment below, we can move more of this logic to class members instead.

@@ -278,6 +284,8 @@ def test_ensure_copied_data(self):

index_type = index.__class__
result = index_type(index.values, copy=True, **init_kwargs)
result = self._fix_tz(result, index)
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to touch this because _holder will also be different.

We shold prob refactor this init_kwargs bizness to instead be a class method, that way each class can construct things easily

IOW

class .....:

     # e.g. for PeriodIndex
     _init_kwargs = lambda index: return {'freq': index.freq'}

     # in the base class (no-op)
     _init_kwargs = lambda index: return {}

Copy link
Author

@azjps azjps Sep 28, 2017

Choose a reason for hiding this comment

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

_holder here will also be DatetimeIndex, which isn't sufficient because DatetimeIndex constructor doesn't have TZ information. The problem is that the constructor DatetimeIndex(index.values, ...) is getting a numpy datetime64 array of tz-naive timestamps. There currently doesn't seem to be an argument to DatetimeIndex's constructor to tell it what timezone to convert to presuming that the input data is UTC (the tz= argument localizes to the specified timezone), so there's not currently a way to do this through init_kwargs. Hence why I currently call _fix_tz to first localize to UTC and then convert to the TZ of create_index().

To sum up:

  • index = create_index() has TZ = US/Eastern
  • result = index_type(index.values) has TZ = None
  • result = index_type(index.values, tz=index.tz) has TZ = US/Eastern, but because of re-localization has been shifted again, and thus has differently underlying values. ❌
  • self._fix_tz(result, index) does have TZ = US/Eastern

I can do the suggested refactor though.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes prob need to refactor a bit,

@jreback
Copy link
Contributor

jreback commented Nov 12, 2017

closing as stale, but opened an issue to track

@jreback jreback closed this Nov 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants