Skip to content

API: Return Index from DatetimeIndex/PeriodIndex.strftime #20240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ Other API Changes
- ``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`)
- ``pd.to_datetime('today')`` now returns a datetime, consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`)
- :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`)
- :func:`DatetimeIndex.strftime` and :func:`PeriodIndex.strftime` now return an ``Index`` instead of a numpy array to be consistent with similar accessors (:issue:`20127`)

.. _whatsnew_0230.deprecations:

Expand Down
12 changes: 6 additions & 6 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class DatelikeOps(object):
""" common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex """

def strftime(self, date_format):
return np.asarray(self.format(date_format=date_format),
dtype=compat.text_type)
return Index(self.format(date_format=date_format),
dtype=compat.text_type)
strftime.__doc__ = """
Convert to string array using specified date_format.
Convert to Index using specified date_format.

Return an array of formatted strings specified by date_format, which
Return an Index of formatted strings specified by date_format, which
supports the same string format as the python standard library. Details
of the string format can be found in `python string format doc <{0}>`__

Expand All @@ -74,8 +74,8 @@ def strftime(self, date_format):

Returns
-------
numpy.ndarray
NumPy array of formatted strings
Index
Index of formatted strings

See Also
--------
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,16 @@ def test_strftime(self):
datetime_index = date_range('20150301', periods=5)
result = datetime_index.strftime("%Y/%m/%d")

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

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

s = Series([datetime(2013, 1, 1, 2, 32, 59), datetime(2013, 1, 2, 14,
32, 1)])
Expand Down