From 81d5a3d83abac7b18369a81df7449a85eb839ba1 Mon Sep 17 00:00:00 2001 From: reidy-p Date: Sat, 10 Mar 2018 15:53:42 +0000 Subject: [PATCH 1/2] API: Return Index from DatetimeIndex/PeriodIndex.strftime --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/core/indexes/datetimelike.py | 12 ++++++------ pandas/tests/series/test_datetime_values.py | 12 ++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index e838afdbbd083..e065e1e4547c7 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -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`) +- ``DatetimeIndex.strftime`` and ``PeriodIndex.strftime`` now return an ``Index`` instead of a numpy array to be consistent with similar accessors (:issue:`20127`) .. _whatsnew_0230.deprecations: diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 1be71ff68c2fb..fda67f8ac51dc 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -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}>`__ @@ -74,8 +74,8 @@ def strftime(self, date_format): Returns ------- - numpy.ndarray - NumPy array of formatted strings + Index + Index of formatted strings See Also -------- diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 3abc0f724db25..47798d0ddd7f5 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -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)]) From af8b19d947e82d47eb23f41a11c035398c6da6c7 Mon Sep 17 00:00:00 2001 From: reidy-p Date: Sun, 11 Mar 2018 22:56:02 +0000 Subject: [PATCH 2/2] fix tests and whatsnew --- doc/source/whatsnew/v0.23.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index e065e1e4547c7..31735063dc687 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -713,7 +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`) -- ``DatetimeIndex.strftime`` and ``PeriodIndex.strftime`` now return an ``Index`` instead of a numpy array to be consistent with similar accessors (:issue:`20127`) +- :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: