Skip to content

Commit 80549c4

Browse files
author
y-p
committed
BUG: sort out unicode/str/repr and py2/3 for Period Objects GH3363
PTF
1 parent 58d4c58 commit 80549c4

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

pandas/tseries/period.py

+40-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import pandas.core.common as com
1515
from pandas.core.common import isnull
16+
from pandas.util import py3compat
1617

1718
from pandas.lib import Timestamp
1819
import pandas.lib as lib
@@ -264,12 +265,49 @@ def __repr__(self):
264265
base, mult = _gfc(self.freq)
265266
formatted = tslib.period_format(self.ordinal, base)
266267
freqstr = _freq_mod._reverse_period_code_map[base]
267-
return u"Period('%s', '%s')" % (formatted, freqstr)
268+
269+
if not py3compat.PY3:
270+
encoding = com.get_option("display.encoding")
271+
formatted = formatted.encode(encoding)
272+
273+
return "Period('%s', '%s')" % (formatted, freqstr)
268274

269275
def __str__(self):
276+
"""
277+
Return a string representation for a particular DataFrame
278+
279+
Invoked by str(df) in both py2/py3.
280+
Yields Bytestring in Py2, Unicode String in py3.
281+
"""
282+
283+
if py3compat.PY3:
284+
return self.__unicode__()
285+
return self.__bytes__()
286+
287+
def __bytes__(self):
288+
"""
289+
Return a string representation for a particular DataFrame
290+
291+
Invoked by bytes(df) in py3 only.
292+
Yields a bytestring in both py2/py3.
293+
"""
294+
encoding = com.get_option("display.encoding")
295+
return self.__unicode__().encode(encoding, 'replace')
296+
297+
def __unicode__(self):
298+
"""
299+
Return a string representation for a particular DataFrame
300+
301+
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
302+
py2/py3.
303+
"""
270304
base, mult = _gfc(self.freq)
271305
formatted = tslib.period_format(self.ordinal, base)
272-
return (u"%s" % formatted)
306+
value = (u"%s" % formatted)
307+
assert type(value) == unicode
308+
309+
return value
310+
273311

274312
def strftime(self, fmt):
275313
"""

0 commit comments

Comments
 (0)