Skip to content

Commit 8ee6570

Browse files
reidy-pjreback
authored andcommitted
API: Return Index from DatetimeIndex/PeriodIndex.strftime (#20240)
1 parent 92524f5 commit 8ee6570

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ Other API Changes
713713
- ``Categorical.fillna`` now validates its ``value`` and ``method`` keyword arguments. It now raises when both or none are specified, matching the behavior of :meth:`Series.fillna` (:issue:`19682`)
714714
- ``pd.to_datetime('today')`` now returns a datetime, consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`)
715715
- :func:`Series.str.replace` now takes an optional `regex` keyword which, when set to ``False``, uses literal string replacement rather than regex replacement (:issue:`16808`)
716+
- :func:`DatetimeIndex.strftime` and :func:`PeriodIndex.strftime` now return an ``Index`` instead of a numpy array to be consistent with similar accessors (:issue:`20127`)
716717

717718
.. _whatsnew_0230.deprecations:
718719

pandas/core/indexes/datetimelike.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class DatelikeOps(object):
5858
""" common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex """
5959

6060
def strftime(self, date_format):
61-
return np.asarray(self.format(date_format=date_format),
62-
dtype=compat.text_type)
61+
return Index(self.format(date_format=date_format),
62+
dtype=compat.text_type)
6363
strftime.__doc__ = """
64-
Convert to string array using specified date_format.
64+
Convert to Index using specified date_format.
6565
66-
Return an array of formatted strings specified by date_format, which
66+
Return an Index of formatted strings specified by date_format, which
6767
supports the same string format as the python standard library. Details
6868
of the string format can be found in `python string format doc <{0}>`__
6969
@@ -74,8 +74,8 @@ def strftime(self, date_format):
7474
7575
Returns
7676
-------
77-
numpy.ndarray
78-
NumPy array of formatted strings
77+
Index
78+
Index of formatted strings
7979
8080
See Also
8181
--------

pandas/tests/series/test_datetime_values.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,16 @@ def test_strftime(self):
355355
datetime_index = date_range('20150301', periods=5)
356356
result = datetime_index.strftime("%Y/%m/%d")
357357

358-
expected = np.array(['2015/03/01', '2015/03/02', '2015/03/03',
359-
'2015/03/04', '2015/03/05'], dtype=np.object_)
358+
expected = Index(['2015/03/01', '2015/03/02', '2015/03/03',
359+
'2015/03/04', '2015/03/05'], dtype=np.object_)
360360
# dtype may be S10 or U10 depending on python version
361-
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
361+
tm.assert_index_equal(result, expected)
362362

363363
period_index = period_range('20150301', periods=5)
364364
result = period_index.strftime("%Y/%m/%d")
365-
expected = np.array(['2015/03/01', '2015/03/02', '2015/03/03',
366-
'2015/03/04', '2015/03/05'], dtype='=U10')
367-
tm.assert_numpy_array_equal(result, expected)
365+
expected = Index(['2015/03/01', '2015/03/02', '2015/03/03',
366+
'2015/03/04', '2015/03/05'], dtype='=U10')
367+
tm.assert_index_equal(result, expected)
368368

369369
s = Series([datetime(2013, 1, 1, 2, 32, 59), datetime(2013, 1, 2, 14,
370370
32, 1)])

0 commit comments

Comments
 (0)