Skip to content

BUG: Fix for DateOffset's reprs. (GH4638) #4868

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 1 commit into from
Sep 27, 2013
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ Bug Fixes
- Fixed wrong check for overlapping in ``DatetimeIndex.union`` (:issue:`4564`)
- Fixed conflict between thousands separator and date parser in csv_parser (:issue:`4678`)
- Fix appending when dtypes are not the same (error showing mixing float/np.datetime64) (:issue:`4993`)
- Fix repr for DateOffset. No longer show duplicate entries in kwds.
Removed unused offset fields. (:issue:`4638`)

pandas 0.12.0
-------------
Expand Down
32 changes: 22 additions & 10 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,31 @@ def __repr__(self):
className = getattr(self, '_outputName', type(self).__name__)
exclude = set(['n', 'inc'])
attrs = []
for attr in self.__dict__:
for attr in sorted(self.__dict__):
if ((attr == 'kwds' and len(self.kwds) == 0)
or attr.startswith('_')):
continue
if attr not in exclude:
attrs.append('='.join((attr, repr(getattr(self, attr)))))
elif attr == 'kwds':
kwds_new = {}
for key in self.kwds:
if not hasattr(self, key):
kwds_new[key] = self.kwds[key]
if len(kwds_new) > 0:
attrs.append('='.join((attr, repr(kwds_new))))
else:
if attr not in exclude:
attrs.append('='.join((attr, repr(getattr(self, attr)))))

if abs(self.n) != 1:
plural = 's'
else:
plural = ''

n_str = ""
if self.n != 1:
n_str = "%s * " % self.n

out = '<%s ' % self.n + className + plural
out = '<%s' % n_str + className + plural
if attrs:
out += ': ' + ', '.join(attrs)
out += '>'
Expand Down Expand Up @@ -247,7 +259,7 @@ def __init__(self, n=1, **kwds):
def rule_code(self):
return 'B'

def __repr__(self):
def __repr__(self): #TODO: Figure out if this should be merged into DateOffset
Copy link
Contributor

Choose a reason for hiding this comment

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

is this actually still a TODO?

Copy link
Contributor

Choose a reason for hiding this comment

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

@cancan101 all I mean is that presumably you'd be making this decision now - right? Otherwise I'll merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I have to take a look to figure out why some/all of the repr code was duplicated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I saw what looked like code duplication but did not have the time to look into it.

if hasattr(self, 'name') and len(self.name):
return self.name

Expand All @@ -261,8 +273,12 @@ def __repr__(self):
plural = 's'
else:
plural = ''

n_str = ""
if self.n != 1:
n_str = "%s * " % self.n

out = '<%s ' % self.n + className + plural
out = '<%s' % n_str + className + plural
if attrs:
out += ': ' + ', '.join(attrs)
out += '>'
Expand Down Expand Up @@ -741,7 +757,6 @@ def __init__(self, n=1, **kwds):
self.n = n
self.startingMonth = kwds.get('startingMonth', 3)

self.offset = BMonthEnd(3)
self.kwds = kwds

def isAnchored(self):
Expand Down Expand Up @@ -803,7 +818,6 @@ def __init__(self, n=1, **kwds):
self.n = n
self.startingMonth = kwds.get('startingMonth', 3)

self.offset = BMonthBegin(3)
self.kwds = kwds

def isAnchored(self):
Expand Down Expand Up @@ -855,7 +869,6 @@ def __init__(self, n=1, **kwds):
self.n = n
self.startingMonth = kwds.get('startingMonth', 3)

self.offset = MonthEnd(3)
self.kwds = kwds

def isAnchored(self):
Expand Down Expand Up @@ -894,7 +907,6 @@ def __init__(self, n=1, **kwds):
self.n = n
self.startingMonth = kwds.get('startingMonth', 3)

self.offset = MonthBegin(3)
self.kwds = kwds

def isAnchored(self):
Expand Down
41 changes: 34 additions & 7 deletions pandas/tseries/tests/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def test_different_normalize_equals(self):
self.assertEqual(offset, offset2)

def test_repr(self):
assert repr(self.offset) == '<1 BusinessDay>'
assert repr(self.offset2) == '<2 BusinessDays>'
self.assertEqual(repr(self.offset), '<BusinessDay>')
assert repr(self.offset2) == '<2 * BusinessDays>'

expected = '<1 BusinessDay: offset=datetime.timedelta(1)>'
expected = '<BusinessDay: offset=datetime.timedelta(1)>'
assert repr(self.offset + timedelta(1)) == expected

def test_with_offset(self):
Expand Down Expand Up @@ -324,10 +324,10 @@ def test_different_normalize_equals(self):
self.assertEqual(offset, offset2)

def test_repr(self):
assert repr(self.offset) == '<1 CustomBusinessDay>'
assert repr(self.offset2) == '<2 CustomBusinessDays>'
assert repr(self.offset) == '<CustomBusinessDay>'
assert repr(self.offset2) == '<2 * CustomBusinessDays>'

expected = '<1 BusinessDay: offset=datetime.timedelta(1)>'
expected = '<BusinessDay: offset=datetime.timedelta(1)>'
assert repr(self.offset + timedelta(1)) == expected

def test_with_offset(self):
Expand Down Expand Up @@ -526,6 +526,11 @@ def assertOnOffset(offset, date, expected):


class TestWeek(unittest.TestCase):
def test_repr(self):
self.assertEqual(repr(Week(weekday=0)), "<Week: weekday=0>")
self.assertEqual(repr(Week(n=-1, weekday=0)), "<-1 * Week: weekday=0>")
self.assertEqual(repr(Week(n=-2, weekday=0)), "<-2 * Weeks: weekday=0>")

def test_corner(self):
self.assertRaises(ValueError, Week, weekday=7)
assertRaisesRegexp(ValueError, "Day must be", Week, weekday=-1)
Expand Down Expand Up @@ -598,6 +603,9 @@ def test_constructor(self):
assertRaisesRegexp(ValueError, "^Day", WeekOfMonth, n=1, week=0, weekday=-1)
assertRaisesRegexp(ValueError, "^Day", WeekOfMonth, n=1, week=0, weekday=7)

def test_repr(self):
self.assertEqual(repr(WeekOfMonth(weekday=1,week=2)), "<WeekOfMonth: week=2, weekday=1>")

def test_offset(self):
date1 = datetime(2011, 1, 4) # 1st Tuesday of Month
date2 = datetime(2011, 1, 11) # 2nd Tuesday of Month
Expand Down Expand Up @@ -895,6 +903,11 @@ def test_onOffset(self):


class TestBQuarterBegin(unittest.TestCase):

def test_repr(self):
self.assertEqual(repr(BQuarterBegin()),"<BusinessQuarterBegin: startingMonth=3>")
self.assertEqual(repr(BQuarterBegin(startingMonth=3)), "<BusinessQuarterBegin: startingMonth=3>")
self.assertEqual(repr(BQuarterBegin(startingMonth=1)), "<BusinessQuarterBegin: startingMonth=1>")

def test_isAnchored(self):
self.assert_(BQuarterBegin(startingMonth=1).isAnchored())
Expand Down Expand Up @@ -981,6 +994,11 @@ def test_offset(self):

class TestBQuarterEnd(unittest.TestCase):

def test_repr(self):
self.assertEqual(repr(BQuarterEnd()),"<BusinessQuarterEnd: startingMonth=3>")
self.assertEqual(repr(BQuarterEnd(startingMonth=3)), "<BusinessQuarterEnd: startingMonth=3>")
self.assertEqual(repr(BQuarterEnd(startingMonth=1)), "<BusinessQuarterEnd: startingMonth=1>")

def test_isAnchored(self):
self.assert_(BQuarterEnd(startingMonth=1).isAnchored())
self.assert_(BQuarterEnd().isAnchored())
Expand Down Expand Up @@ -1083,6 +1101,11 @@ def test_onOffset(self):


class TestQuarterBegin(unittest.TestCase):
def test_repr(self):
self.assertEqual(repr(QuarterBegin()), "<QuarterBegin: startingMonth=3>")
self.assertEqual(repr(QuarterBegin(startingMonth=3)), "<QuarterBegin: startingMonth=3>")
self.assertEqual(repr(QuarterBegin(startingMonth=1)),"<QuarterBegin: startingMonth=1>")

def test_isAnchored(self):
self.assert_(QuarterBegin(startingMonth=1).isAnchored())
self.assert_(QuarterBegin().isAnchored())
Expand Down Expand Up @@ -1152,7 +1175,11 @@ def test_offset(self):


class TestQuarterEnd(unittest.TestCase):

def test_repr(self):
self.assertEqual(repr(QuarterEnd()), "<QuarterEnd: startingMonth=3>")
self.assertEqual(repr(QuarterEnd(startingMonth=3)), "<QuarterEnd: startingMonth=3>")
self.assertEqual(repr(QuarterEnd(startingMonth=1)), "<QuarterEnd: startingMonth=1>")

def test_isAnchored(self):
self.assert_(QuarterEnd(startingMonth=1).isAnchored())
self.assert_(QuarterEnd().isAnchored())
Expand Down