Skip to content

isolate scalar Timestamp tests from date_range tests #17957

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

Merged
merged 7 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 66 additions & 44 deletions pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ def test_pprint(self):
'foo': 1}"""
assert result == expected

def to_datetime_depr(self):
def test_to_datetime_depr(self):
# see gh-8254
assert False
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the issue here? I guess this test wasn't turned on before.

Copy link
Member Author

Choose a reason for hiding this comment

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

Woops, yah. That was an earlier check that these were not getting hit. Changing now.

ts = Timestamp('2011-01-01')

with tm.assert_produces_warning(FutureWarning,
Expand All @@ -592,7 +593,8 @@ def to_datetime_depr(self):
result = ts.to_datetime()
assert result == expected

def to_pydatetime_nonzero_nano(self):
def test_to_pydatetime_nonzero_nano(self):
assert False
Copy link
Contributor

Choose a reason for hiding this comment

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

same?

ts = Timestamp('2011-01-01 9:00:00.123456789')

# Warn the user of data loss (nanoseconds).
Expand Down Expand Up @@ -969,35 +971,6 @@ def test_delta_preserve_nanos(self):
result = val + timedelta(1)
assert result.nanosecond == val.nanosecond

def test_frequency_misc(self):
assert (frequencies.get_freq_group('T') ==
frequencies.FreqGroup.FR_MIN)

code, stride = frequencies.get_freq_code(offsets.Hour())
assert code == frequencies.FreqGroup.FR_HR

code, stride = frequencies.get_freq_code((5, 'T'))
assert code == frequencies.FreqGroup.FR_MIN
assert stride == 5

offset = offsets.Hour()
result = frequencies.to_offset(offset)
assert result == offset

result = frequencies.to_offset((5, 'T'))
expected = offsets.Minute(5)
assert result == expected

pytest.raises(ValueError, frequencies.get_freq_code, (5, 'baz'))

pytest.raises(ValueError, frequencies.to_offset, '100foo')

pytest.raises(ValueError, frequencies.to_offset, ('', ''))

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = frequencies.get_standard_freq(offsets.Hour())
assert result == 'H'

def test_hash_equivalent(self):
d = {datetime(2011, 1, 1): 5}
stamp = Timestamp(datetime(2011, 1, 1))
Expand Down Expand Up @@ -1268,26 +1241,19 @@ def test_compare_hour13(self):
class TestTimeSeries(object):

def test_timestamp_to_datetime(self):
rng = date_range('20090415', '20090519', tz='US/Eastern')

stamp = rng[0]
stamp = Timestamp('20090415', tz='US/Eastern', freq='D')
dtval = stamp.to_pydatetime()
assert stamp == dtval
assert stamp.tzinfo == dtval.tzinfo

def test_timestamp_to_datetime_dateutil(self):
rng = date_range('20090415', '20090519', tz='dateutil/US/Eastern')

stamp = rng[0]
stamp = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D')
dtval = stamp.to_pydatetime()
assert stamp == dtval
assert stamp.tzinfo == dtval.tzinfo

def test_timestamp_to_datetime_explicit_pytz(self):
rng = date_range('20090415', '20090519',
tz=pytz.timezone('US/Eastern'))

stamp = rng[0]
stamp = Timestamp('20090415', tz=pytz.timezone('US/Eastern'), freq='D')
dtval = stamp.to_pydatetime()
assert stamp == dtval
assert stamp.tzinfo == dtval.tzinfo
Expand All @@ -1296,9 +1262,7 @@ def test_timestamp_to_datetime_explicit_dateutil(self):
tm._skip_if_windows_python_3()

from pandas._libs.tslibs.timezones import dateutil_gettz as gettz
rng = date_range('20090415', '20090519', tz=gettz('US/Eastern'))

stamp = rng[0]
stamp = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D')
dtval = stamp.to_pydatetime()
assert stamp == dtval
assert stamp.tzinfo == dtval.tzinfo
Expand Down Expand Up @@ -1494,3 +1458,61 @@ def test_to_datetime_bijective(self):
with tm.assert_produces_warning(exp_warning, check_stacklevel=False):
assert (Timestamp(Timestamp.min.to_pydatetime()).value / 1000 ==
Timestamp.min.value / 1000)


class TestTimestampEquivDateRange(object):
# Older tests in TestTimeSeries constructed their `stamp` objects
# using `date_range` instead of the `Timestamp` constructor.
# TestTimestampEquivDateRange checks that these are equivalent in the
# pertinent cases.

def test_date_range_timestamp_equiv(self):
rng = date_range('20090415', '20090519', tz='US/Eastern')
stamp = rng[0]
Copy link
Member

@gfyoung gfyoung Oct 23, 2017

Choose a reason for hiding this comment

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

Given that you use this construct several times across tests (i.e. rng = date_range(...)[0]), you might want to consider making that a separate method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Given the large number of irons in the fire at the moment, would you be OK with these comments going into #17652 and being addressed in a follow-up?

Copy link
Member

Choose a reason for hiding this comment

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

@jbrockmendel : I would prefer to make a push to refactor now, as this is a reorganization PR after all. Save it in the end as a last commit to this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would prefer to make a push to refactor now, as this is a reorganization PR after all. Save it in the end as a last commit to this PR.

Alright, let's wrap this thing up! Thanks for taking the time to review.

Given that you use this construct several times across tests (i.e. rng = date_range(...)[0]), you might want to consider making that a separate method.

Seems like replacing a 1-line function call with a 1-line method call doesn't really gain anything, no?


ts = Timestamp('20090415', tz='US/Eastern', freq='D')
assert ts == stamp

def test_date_range_timestamp_equiv_dateutil(self):
rng = date_range('20090415', '20090519', tz='dateutil/US/Eastern')
stamp = rng[0]

ts = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D')
assert ts == stamp

def test_date_range_timestamp_equiv_explicit_pytz(self):
rng = date_range('20090415', '20090519',
tz=pytz.timezone('US/Eastern'))
stamp = rng[0]

ts = Timestamp('20090415', tz=pytz.timezone('US/Eastern'), freq='D')
assert ts == stamp

def test_date_range_timestamp_equiv_explicit_dateutil(self):
tm._skip_if_windows_python_3()
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz

rng = date_range('20090415', '20090519', tz=gettz('US/Eastern'))
stamp = rng[0]

ts = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D')
assert ts == stamp

def test_date_range_timestamp_equiv_from_datetime_instance(self):
# This test refers to TestTimestampOps.test_addition_subtraction_types
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if comments like these are necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove.

datetime_instance = datetime(2014, 3, 4)
# build a timestamp with a frequency, since then it supports
# addition/subtraction of integers
timestamp_instance = date_range(datetime_instance, periods=1,
freq='D')[0]

ts = Timestamp(datetime_instance, freq='D')
assert ts == timestamp_instance

def test_date_range_timestamp_equiv_preserve_frequency(self):
# This test refers to
# TestTimestampOps.test_addition_subtraction_preserve_frequency
timestamp_instance = date_range('2014-03-05', periods=1, freq='D')[0]
ts = Timestamp('2014-03-05', freq='D')

assert timestamp_instance == ts
32 changes: 32 additions & 0 deletions pandas/tests/tseries/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,38 @@ def test_get_freq_code(self):
assert (frequencies.get_freq_code(offsets.Week(-2, weekday=4)) ==
(frequencies.get_freq('W-FRI'), -2))

def test_frequency_misc(self):
assert (frequencies.get_freq_group('T') ==
frequencies.FreqGroup.FR_MIN)

code, stride = frequencies.get_freq_code(offsets.Hour())
assert code == frequencies.FreqGroup.FR_HR

code, stride = frequencies.get_freq_code((5, 'T'))
assert code == frequencies.FreqGroup.FR_MIN
assert stride == 5

offset = offsets.Hour()
result = frequencies.to_offset(offset)
assert result == offset

result = frequencies.to_offset((5, 'T'))
expected = offsets.Minute(5)
assert result == expected

with tm.assert_raises_regex(ValueError, 'Invalid frequency'):
frequencies.get_freq_code((5, 'baz'))

with tm.assert_raises_regex(ValueError, 'Invalid frequency'):
frequencies.to_offset('100foo')

with tm.assert_raises_regex(ValueError, 'Could not evaluate'):
frequencies.to_offset(('', ''))

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = frequencies.get_standard_freq(offsets.Hour())
assert result == 'H'


_dti = DatetimeIndex

Expand Down