Skip to content

Commit 595f9fa

Browse files
committed
API: Return Index from DatetimeIndex/PeriodIndex.strftime
1 parent c3d491a commit 595f9fa

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ Other API Changes
711711
- ``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`)
712712
- ``pd.to_datetime('today')`` now returns a datetime, consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`)
713713
- :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`)
714+
- ``DatetimeIndex.strftime`` and ``PeriodIndex.strftime`` now return an ``Index`` instead of a numpy array to be consistent with similar accessors (:issue:`20127`)
714715

715716
.. _whatsnew_0230.deprecations:
716717

pandas/core/indexes/datetimelike.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ 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-
Return an array of formatted strings specified by date_format, which
64+
Return an Index of formatted strings specified by date_format, which
6565
supports the same string format as the python standard library. Details
6666
of the string format can be found in `python string format doc <{0}>`__
6767
@@ -72,7 +72,7 @@ def strftime(self, date_format):
7272
7373
Returns
7474
-------
75-
ndarray of formatted strings
75+
Index of formatted strings
7676
""".format("https://docs.python.org/3/library/datetime.html"
7777
"#strftime-and-strptime-behavior")
7878

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)