Skip to content

TST: start tests for PeriodConverter #9055

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
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
4 changes: 2 additions & 2 deletions pandas/tseries/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PeriodConverter(dates.DateConverter):
def convert(values, units, axis):
if not hasattr(axis, 'freq'):
raise TypeError('Axis must have `freq` set to convert to Periods')
valid_types = (str, datetime, Period, pydt.date, pydt.time)
valid_types = (compat.string_types, datetime, Period, pydt.date, pydt.time)
if (isinstance(values, valid_types) or com.is_integer(values) or
com.is_float(values)):
return get_datevalue(values, axis.freq)
Expand All @@ -127,7 +127,7 @@ def convert(values, units, axis):
def get_datevalue(date, freq):
if isinstance(date, Period):
return date.asfreq(freq).ordinal
elif isinstance(date, (str, datetime, pydt.date, pydt.time)):
elif isinstance(date, (compat.string_types, datetime, pydt.date, pydt.time)):
return Period(date, freq).ordinal
elif (com.is_integer(date) or com.is_float(date) or
(isinstance(date, (np.ndarray, Index)) and (date.size == 1))):
Expand Down
56 changes: 55 additions & 1 deletion pandas/tseries/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np
from numpy.testing import assert_almost_equal as np_assert_almost_equal
from pandas import Timestamp
from pandas import Timestamp, Period
from pandas.compat import u
import pandas.util.testing as tm
from pandas.tseries.offsets import Second, Milli, Micro
Expand Down Expand Up @@ -103,6 +103,60 @@ def _assert_less(ts1, ts2):
_assert_less(ts, ts + Micro(50))


class TestPeriodConverter(tm.TestCase):

def setUp(self):
self.pc = converter.PeriodConverter()

class Axis(object):
pass

self.axis = Axis()
self.axis.freq = 'D'

def test_convert_accepts_unicode(self):
r1 = self.pc.convert("2012-1-1", None, self.axis)
r2 = self.pc.convert(u("2012-1-1"), None, self.axis)
self.assert_equal(r1, r2, "PeriodConverter.convert should accept unicode")
Copy link
Contributor

Choose a reason for hiding this comment

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

Never used PeriodConverter but the undelrying supports, so maybe just a small issue

In [1]: pd.Timestamp(u'20121201')
Out[1]: Timestamp('2012-12-01 00:00:00')

In [2]: pd.Period(u'20121201',freq='D')
Out[2]: Period('2012-12-01', 'D')

Copy link
Member Author

Choose a reason for hiding this comment

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

It is actually a quite simple reason. From https://github.com/pydata/pandas/blob/master/pandas/tseries/converter.py#L112:

valid_types = (str, datetime, Period, pydt.date, pydt.time)
if isinstance(values, valid_types):
....

here of course testing for str does not work for unicode

I will add this

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 just have to use pd.compat.string_types instead of str?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep


def test_conversion(self):
rs = self.pc.convert(['2012-1-1'], None, self.axis)[0]
xp = Period('2012-1-1').ordinal
self.assertEqual(rs, xp)

rs = self.pc.convert('2012-1-1', None, self.axis)
self.assertEqual(rs, xp)

rs = self.pc.convert([date(2012, 1, 1)], None, self.axis)[0]
self.assertEqual(rs, xp)

rs = self.pc.convert(date(2012, 1, 1), None, self.axis)
self.assertEqual(rs, xp)

rs = self.pc.convert([Timestamp('2012-1-1')], None, self.axis)[0]
self.assertEqual(rs, xp)

rs = self.pc.convert(Timestamp('2012-1-1'), None, self.axis)
self.assertEqual(rs, xp)

# FIXME
# rs = self.pc.convert(np.datetime64('2012-01-01'), None, self.axis)
# self.assertEqual(rs, xp)
#
# rs = self.pc.convert(np.datetime64('2012-01-01 00:00:00+00:00'), None, self.axis)
# self.assertEqual(rs, xp)
#
# rs = self.pc.convert(np.array([np.datetime64('2012-01-01 00:00:00+00:00'),
# np.datetime64('2012-01-02 00:00:00+00:00')]), None, self.axis)
# self.assertEqual(rs[0], xp)

def test_integer_passthrough(self):
# GH9012
rs = self.pc.convert([0, 1], None, self.axis)
xp = [0, 1]
self.assertEqual(rs, xp)


if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down