|
14 | 14 | import pandas.compat as compat
|
15 | 15 | from pandas.errors import (
|
16 | 16 | AbstractMethodError, NullFrequencyError, PerformanceWarning)
|
17 |
| -from pandas.util._decorators import Appender, deprecate_kwarg |
| 17 | +from pandas.util._decorators import Appender, Substitution, deprecate_kwarg |
18 | 18 |
|
19 | 19 | from pandas.core.dtypes.common import (
|
20 | 20 | is_bool_dtype, is_datetime64_any_dtype, is_datetime64_dtype,
|
@@ -86,44 +86,45 @@ class DatelikeOps(object):
|
86 | 86 | Common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex.
|
87 | 87 | """
|
88 | 88 |
|
| 89 | + @Substitution(URL="https://docs.python.org/3/library/datetime.html" |
| 90 | + "#strftime-and-strptime-behavior") |
89 | 91 | 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. |
95 | 94 |
|
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>`__ |
99 | 99 |
|
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"). |
104 | 104 |
|
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)) |
127 | 128 |
|
128 | 129 |
|
129 | 130 | class TimelikeOps(object):
|
@@ -298,6 +299,23 @@ def asi8(self):
|
298 | 299 | # do not cache or you'll create a memory leak
|
299 | 300 | return self._data.view('i8')
|
300 | 301 |
|
| 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 | + |
301 | 319 | # ----------------------------------------------------------------
|
302 | 320 | # Array-Like / EA-Interface Methods
|
303 | 321 |
|
|
0 commit comments