|
13 | 13 |
|
14 | 14 | import pandas.core.common as com
|
15 | 15 | from pandas.core.common import isnull
|
| 16 | +from pandas.util import py3compat |
16 | 17 |
|
17 | 18 | from pandas.lib import Timestamp
|
18 | 19 | import pandas.lib as lib
|
@@ -264,12 +265,49 @@ def __repr__(self):
|
264 | 265 | base, mult = _gfc(self.freq)
|
265 | 266 | formatted = tslib.period_format(self.ordinal, base)
|
266 | 267 | 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) |
268 | 274 |
|
269 | 275 | 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 | + """ |
270 | 304 | base, mult = _gfc(self.freq)
|
271 | 305 | 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 | + |
273 | 311 |
|
274 | 312 | def strftime(self, fmt):
|
275 | 313 | """
|
|
0 commit comments