Skip to content

Commit 2a28e53

Browse files
jbrockmendelPingviinituutti
authored andcommitted
Rendering Methods portions of pandas-dev#24024 (pandas-dev#24392)
1 parent 60d1166 commit 2a28e53

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

pandas/core/arrays/datetimelike.py

+53-35
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pandas.compat as compat
1515
from pandas.errors import (
1616
AbstractMethodError, NullFrequencyError, PerformanceWarning)
17-
from pandas.util._decorators import Appender, deprecate_kwarg
17+
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg
1818

1919
from pandas.core.dtypes.common import (
2020
is_bool_dtype, is_datetime64_any_dtype, is_datetime64_dtype,
@@ -86,44 +86,45 @@ class DatelikeOps(object):
8686
Common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex.
8787
"""
8888

89+
@Substitution(URL="https://docs.python.org/3/library/datetime.html"
90+
"#strftime-and-strptime-behavior")
8991
def strftime(self, date_format):
90-
from pandas import Index
91-
return Index(self.format(date_format=date_format),
92-
dtype=compat.text_type)
93-
strftime.__doc__ = """
94-
Convert to Index using specified date_format.
92+
"""
93+
Convert to Index using specified date_format.
9594
96-
Return an Index of formatted strings specified by date_format, which
97-
supports the same string format as the python standard library. Details
98-
of the string format can be found in `python string format doc <{0}>`__
95+
Return an Index of formatted strings specified by date_format, which
96+
supports the same string format as the python standard library. Details
97+
of the string format can be found in `python string format
98+
doc <%(URL)s>`__
9999
100-
Parameters
101-
----------
102-
date_format : str
103-
Date format string (e.g. "%Y-%m-%d").
100+
Parameters
101+
----------
102+
date_format : str
103+
Date format string (e.g. "%%Y-%%m-%%d").
104104
105-
Returns
106-
-------
107-
Index
108-
Index of formatted strings
109-
110-
See Also
111-
--------
112-
to_datetime : Convert the given argument to datetime.
113-
DatetimeIndex.normalize : Return DatetimeIndex with times to midnight.
114-
DatetimeIndex.round : Round the DatetimeIndex to the specified freq.
115-
DatetimeIndex.floor : Floor the DatetimeIndex to the specified freq.
116-
117-
Examples
118-
--------
119-
>>> rng = pd.date_range(pd.Timestamp("2018-03-10 09:00"),
120-
... periods=3, freq='s')
121-
>>> rng.strftime('%B %d, %Y, %r')
122-
Index(['March 10, 2018, 09:00:00 AM', 'March 10, 2018, 09:00:01 AM',
123-
'March 10, 2018, 09:00:02 AM'],
124-
dtype='object')
125-
""".format("https://docs.python.org/3/library/datetime.html"
126-
"#strftime-and-strptime-behavior")
105+
Returns
106+
-------
107+
Index
108+
Index of formatted strings
109+
110+
See Also
111+
--------
112+
to_datetime : Convert the given argument to datetime.
113+
DatetimeIndex.normalize : Return DatetimeIndex with times to midnight.
114+
DatetimeIndex.round : Round the DatetimeIndex to the specified freq.
115+
DatetimeIndex.floor : Floor the DatetimeIndex to the specified freq.
116+
117+
Examples
118+
--------
119+
>>> rng = pd.date_range(pd.Timestamp("2018-03-10 09:00"),
120+
... periods=3, freq='s')
121+
>>> rng.strftime('%%B %%d, %%Y, %%r')
122+
Index(['March 10, 2018, 09:00:00 AM', 'March 10, 2018, 09:00:01 AM',
123+
'March 10, 2018, 09:00:02 AM'],
124+
dtype='object')
125+
"""
126+
from pandas import Index
127+
return Index(self._format_native_types(date_format=date_format))
127128

128129

129130
class TimelikeOps(object):
@@ -298,6 +299,23 @@ def asi8(self):
298299
# do not cache or you'll create a memory leak
299300
return self._data.view('i8')
300301

302+
# ----------------------------------------------------------------
303+
# Rendering Methods
304+
305+
def _format_native_types(self, na_rep=u'NaT', date_format=None):
306+
"""
307+
Helper method for astype when converting to strings.
308+
309+
Returns
310+
-------
311+
ndarray[str]
312+
"""
313+
raise AbstractMethodError(self)
314+
315+
def _formatter(self, boxed=False):
316+
# TODO: Remove Datetime & DatetimeTZ formatters.
317+
return "'{}'".format
318+
301319
# ----------------------------------------------------------------
302320
# Array-Like / EA-Interface Methods
303321

pandas/core/arrays/datetimes.py

+12
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ def _validate_fill_value(self, fill_value):
468468
"Got '{got}'.".format(got=fill_value))
469469
return fill_value
470470

471+
# -----------------------------------------------------------------
472+
# Rendering Methods
473+
474+
def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
475+
from pandas.io.formats.format import _get_format_datetime64_from_values
476+
fmt = _get_format_datetime64_from_values(self, date_format)
477+
478+
return tslib.format_array_from_datetime(self.asi8,
479+
tz=self.tz,
480+
format=fmt,
481+
na_rep=na_rep)
482+
471483
# -----------------------------------------------------------------
472484
# Comparison Methods
473485

pandas/core/arrays/period.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def wrapper(self, other):
9292
return compat.set_function_name(wrapper, opname, cls)
9393

9494

95-
class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray):
95+
class PeriodArray(dtl.DatetimeLikeArrayMixin,
96+
dtl.DatelikeOps,
97+
ExtensionArray):
9698
"""
9799
Pandas ExtensionArray for storing Period data.
98100
@@ -565,7 +567,7 @@ def asfreq(self, freq=None, how='E'):
565567
return type(self)(new_data, freq=freq)
566568

567569
# ------------------------------------------------------------------
568-
# Formatting
570+
# Rendering Methods
569571

570572
def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
571573
"""
@@ -589,9 +591,7 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
589591
values = np.array([formatter(dt) for dt in values])
590592
return values
591593

592-
# Delegation...
593-
def strftime(self, date_format):
594-
return self._format_native_types(date_format=date_format)
594+
# ------------------------------------------------------------------
595595

596596
def repeat(self, repeats, *args, **kwargs):
597597
"""

pandas/core/arrays/timedeltas.py

+10
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ def _validate_fill_value(self, fill_value):
231231
"Got '{got}'.".format(got=fill_value))
232232
return fill_value
233233

234+
# ----------------------------------------------------------------
235+
# Rendering Methods
236+
237+
def _formatter(self, boxed=False):
238+
from pandas.io.formats.format import _get_format_timedelta64
239+
return _get_format_timedelta64(self, box=True)
240+
241+
def _format_native_types(self):
242+
return self.astype(object)
243+
234244
# ----------------------------------------------------------------
235245
# Arithmetic Methods
236246

0 commit comments

Comments
 (0)