Skip to content

collect index formatting tests #19661

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 4 commits into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
175 changes: 174 additions & 1 deletion pandas/tests/indexes/datetimes/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from pandas import DatetimeIndex
from datetime import datetime
from pandas import DatetimeIndex, Series

import numpy as np
import dateutil.tz
import pytz
import pytest

import pandas.util.testing as tm
import pandas as pd
Expand Down Expand Up @@ -45,3 +49,172 @@ def test_to_native_types():

result = index.to_native_types(na_rep='pandas')
tm.assert_numpy_array_equal(result, expected)


class TestDatetimeIndexRendering(object):
def test_dti_repr_short(self):
dr = pd.date_range(start='1/1/2012', periods=1)
repr(dr)

dr = pd.date_range(start='1/1/2012', periods=2)
repr(dr)

dr = pd.date_range(start='1/1/2012', periods=3)
repr(dr)

@pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__'])
def test_dti_representation(self, method):
idxs = []
idxs.append(DatetimeIndex([], freq='D'))
idxs.append(DatetimeIndex(['2011-01-01'], freq='D'))
idxs.append(DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D'))
idxs.append(DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'],
freq='D'))
idxs.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'
], freq='H', tz='Asia/Tokyo'))
idxs.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern'))
idxs.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='UTC'))

exp = []
exp.append("""DatetimeIndex([], dtype='datetime64[ns]', freq='D')""")
exp.append("DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', "
"freq='D')")
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02'], "
"dtype='datetime64[ns]', freq='D')")
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
"dtype='datetime64[ns]', freq='D')")
exp.append("DatetimeIndex(['2011-01-01 09:00:00+09:00', "
"'2011-01-01 10:00:00+09:00', '2011-01-01 11:00:00+09:00']"
", dtype='datetime64[ns, Asia/Tokyo]', freq='H')")
exp.append("DatetimeIndex(['2011-01-01 09:00:00-05:00', "
"'2011-01-01 10:00:00-05:00', 'NaT'], "
"dtype='datetime64[ns, US/Eastern]', freq=None)")
exp.append("DatetimeIndex(['2011-01-01 09:00:00+00:00', "
"'2011-01-01 10:00:00+00:00', 'NaT'], "
"dtype='datetime64[ns, UTC]', freq=None)""")

with pd.option_context('display.width', 300):
Copy link
Contributor

Choose a reason for hiding this comment

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

looks fine. can you parameterize these (and others that look similar)

Copy link
Member Author

Choose a reason for hiding this comment

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

Managed to parametrize a couple of for loops. There is probably more than can be done in the next pass.

for indx, expected in zip(idxs, exp):
result = getattr(indx, method)()
assert result == expected

def test_dti_representation_to_series(self):
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
'2011-01-01 11:00'], freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')
idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15'])

exp1 = """Series([], dtype: datetime64[ns])"""

exp2 = ("0 2011-01-01\n"
"dtype: datetime64[ns]")

exp3 = ("0 2011-01-01\n"
"1 2011-01-02\n"
"dtype: datetime64[ns]")

exp4 = ("0 2011-01-01\n"
"1 2011-01-02\n"
"2 2011-01-03\n"
"dtype: datetime64[ns]")

exp5 = ("0 2011-01-01 09:00:00+09:00\n"
"1 2011-01-01 10:00:00+09:00\n"
"2 2011-01-01 11:00:00+09:00\n"
"dtype: datetime64[ns, Asia/Tokyo]")

exp6 = ("0 2011-01-01 09:00:00-05:00\n"
"1 2011-01-01 10:00:00-05:00\n"
"2 NaT\n"
"dtype: datetime64[ns, US/Eastern]")

exp7 = ("0 2011-01-01 09:00:00\n"
"1 2011-01-02 10:15:00\n"
"dtype: datetime64[ns]")

with pd.option_context('display.width', 300):
for idx, expected in zip([idx1, idx2, idx3, idx4,
idx5, idx6, idx7],
[exp1, exp2, exp3, exp4,
exp5, exp6, exp7]):
result = repr(Series(idx))
assert result == expected

def test_dti_summary(self):
# GH#9116
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
'2011-01-01 11:00'],
freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')

exp1 = ("DatetimeIndex: 0 entries\n"
"Freq: D")

exp2 = ("DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01\n"
"Freq: D")

exp3 = ("DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02\n"
"Freq: D")

exp4 = ("DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03\n"
"Freq: D")

exp5 = ("DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 "
"to 2011-01-01 11:00:00+09:00\n"
"Freq: H")

exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6],
[exp1, exp2, exp3, exp4, exp5, exp6]):
result = idx.summary()
assert result == expected

def test_dti_business_repr(self):
# only really care that it works
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1)))

def test_dti_business_summary(self):
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))
rng.summary()
rng[2:2].summary()

def test_dti_business_summary_pytz(self):
pd.bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()

def test_dti_business_summary_dateutil(self):
pd.bdate_range('1/1/2005', '1/1/2009',
tz=dateutil.tz.tzutc()).summary()

def test_dti_custom_business_repr(self):
# only really care that it works
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1),
freq='C'))

def test_dti_custom_business_summary(self):
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1),
freq='C')
rng.summary()
rng[2:2].summary()

def test_dti_custom_business_summary_pytz(self):
pd.bdate_range('1/1/2005', '1/1/2009', freq='C', tz=pytz.utc).summary()

def test_dti_custom_business_summary_dateutil(self):
pd.bdate_range('1/1/2005', '1/1/2009', freq='C',
tz=dateutil.tz.tzutc()).summary()
10 changes: 0 additions & 10 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ def test_range_edges(self):
'1970-01-03', '1970-01-04'])
tm.assert_index_equal(idx, exp)

def test_datetimeindex_repr_short(self):
dr = date_range(start='1/1/2012', periods=1)
repr(dr)

dr = date_range(start='1/1/2012', periods=2)
repr(dr)

dr = date_range(start='1/1/2012', periods=3)
repr(dr)


class TestDatetime64(object):

Expand Down
155 changes: 0 additions & 155 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pytz
import pytest
import dateutil
import warnings
import numpy as np
from datetime import datetime
Expand Down Expand Up @@ -153,130 +151,6 @@ def test_repeat(self):
tm.assert_raises_regex(ValueError, msg, np.repeat,
rng, reps, axis=1)

def test_representation(self):

idx = []
idx.append(DatetimeIndex([], freq='D'))
idx.append(DatetimeIndex(['2011-01-01'], freq='D'))
idx.append(DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D'))
idx.append(DatetimeIndex(
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D'))
idx.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'
], freq='H', tz='Asia/Tokyo'))
idx.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern'))
idx.append(DatetimeIndex(
['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='UTC'))

exp = []
exp.append("""DatetimeIndex([], dtype='datetime64[ns]', freq='D')""")
exp.append("DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', "
"freq='D')")
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02'], "
"dtype='datetime64[ns]', freq='D')")
exp.append("DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
"dtype='datetime64[ns]', freq='D')")
exp.append("DatetimeIndex(['2011-01-01 09:00:00+09:00', "
"'2011-01-01 10:00:00+09:00', '2011-01-01 11:00:00+09:00']"
", dtype='datetime64[ns, Asia/Tokyo]', freq='H')")
exp.append("DatetimeIndex(['2011-01-01 09:00:00-05:00', "
"'2011-01-01 10:00:00-05:00', 'NaT'], "
"dtype='datetime64[ns, US/Eastern]', freq=None)")
exp.append("DatetimeIndex(['2011-01-01 09:00:00+00:00', "
"'2011-01-01 10:00:00+00:00', 'NaT'], "
"dtype='datetime64[ns, UTC]', freq=None)""")

with pd.option_context('display.width', 300):
for indx, expected in zip(idx, exp):
for func in ['__repr__', '__unicode__', '__str__']:
result = getattr(indx, func)()
assert result == expected

def test_representation_to_series(self):
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
'2011-01-01 11:00'], freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')
idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15'])

exp1 = """Series([], dtype: datetime64[ns])"""

exp2 = ("0 2011-01-01\n"
"dtype: datetime64[ns]")

exp3 = ("0 2011-01-01\n"
"1 2011-01-02\n"
"dtype: datetime64[ns]")

exp4 = ("0 2011-01-01\n"
"1 2011-01-02\n"
"2 2011-01-03\n"
"dtype: datetime64[ns]")

exp5 = ("0 2011-01-01 09:00:00+09:00\n"
"1 2011-01-01 10:00:00+09:00\n"
"2 2011-01-01 11:00:00+09:00\n"
"dtype: datetime64[ns, Asia/Tokyo]")

exp6 = ("0 2011-01-01 09:00:00-05:00\n"
"1 2011-01-01 10:00:00-05:00\n"
"2 NaT\n"
"dtype: datetime64[ns, US/Eastern]")

exp7 = ("0 2011-01-01 09:00:00\n"
"1 2011-01-02 10:15:00\n"
"dtype: datetime64[ns]")

with pd.option_context('display.width', 300):
for idx, expected in zip([idx1, idx2, idx3, idx4,
idx5, idx6, idx7],
[exp1, exp2, exp3, exp4,
exp5, exp6, exp7]):
result = repr(Series(idx))
assert result == expected

def test_summary(self):
# GH9116
idx1 = DatetimeIndex([], freq='D')
idx2 = DatetimeIndex(['2011-01-01'], freq='D')
idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
idx4 = DatetimeIndex(
['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')
idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00',
'2011-01-01 11:00'],
freq='H', tz='Asia/Tokyo')
idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT],
tz='US/Eastern')

exp1 = ("DatetimeIndex: 0 entries\n"
"Freq: D")

exp2 = ("DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01\n"
"Freq: D")

exp3 = ("DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02\n"
"Freq: D")

exp4 = ("DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03\n"
"Freq: D")

exp5 = ("DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 "
"to 2011-01-01 11:00:00+09:00\n"
"Freq: H")

exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT"""

for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6],
[exp1, exp2, exp3, exp4, exp5, exp6]):
result = idx.summary()
assert result == expected

def test_resolution(self):
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T',
'S', 'L', 'U'],
Expand Down Expand Up @@ -544,10 +418,6 @@ def test_copy(self):
repr(cp)
tm.assert_index_equal(cp, self.rng)

def test_repr(self):
# only really care that it works
repr(self.rng)

def test_shift(self):
shifted = self.rng.shift(5)
assert shifted[0] == self.rng[5]
Expand All @@ -565,16 +435,6 @@ def test_shift(self):
shifted = rng.shift(1, freq=BDay())
assert shifted[0] == rng[0] + BDay()

def test_summary(self):
self.rng.summary()
self.rng[2:2].summary()

def test_summary_pytz(self):
bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()

def test_summary_dateutil(self):
bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()

def test_equals(self):
assert not self.rng.equals(list(self.rng))

Expand Down Expand Up @@ -612,10 +472,6 @@ def test_copy(self):
repr(cp)
tm.assert_index_equal(cp, self.rng)

def test_repr(self):
# only really care that it works
repr(self.rng)

def test_shift(self):

shifted = self.rng.shift(5)
Expand All @@ -640,16 +496,5 @@ def test_pickle_unpickle(self):
unpickled = tm.round_trip_pickle(self.rng)
assert unpickled.offset is not None

def test_summary(self):
self.rng.summary()
self.rng[2:2].summary()

def test_summary_pytz(self):
bdate_range('1/1/2005', '1/1/2009', freq='C', tz=pytz.utc).summary()

def test_summary_dateutil(self):
bdate_range('1/1/2005', '1/1/2009', freq='C',
tz=dateutil.tz.tzutc()).summary()

def test_equals(self):
assert not self.rng.equals(list(self.rng))
Loading