Skip to content

Commit

Permalink
Backport PR pandas-dev#48147: BUG: ensure object dtype for strftime a…
Browse files Browse the repository at this point in the history
…ccessor/index method + REGR: avoid spurious warning
  • Loading branch information
jorisvandenbossche authored and meeseeksmachine committed Aug 19, 2022
1 parent 9040423 commit c14d3e1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bug fixes
- The :class:`errors.FutureWarning` raised when passing arguments (other than ``filepath_or_buffer``) as positional in :func:`read_csv` is now raised at the correct stacklevel (:issue:`47385`)
- Bug in :meth:`DataFrame.to_sql` when ``method`` was a ``callable`` that did not return an ``int`` and would raise a ``TypeError`` (:issue:`46891`)
- Bug in :meth:`loc.__getitem__` with a list of keys causing an internal inconsistency that could lead to a disconnect between ``frame.at[x, y]`` vs ``frame[y].loc[x]`` (:issue:`22372`)
- Bug in the :meth:`Series.dt.strftime` accessor return a float instead of object dtype Series for all-NaT input, which also causes a spurious deprecation warning (:issue:`45858`)

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _new_DatetimeIndex(cls, d):
+ [
method
for method in DatetimeArray._datetimelike_methods
if method not in ("tz_localize", "tz_convert")
if method not in ("tz_localize", "tz_convert", "strftime")
],
DatetimeArray,
wrap=True,
Expand Down Expand Up @@ -261,7 +261,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
@doc(DatetimeArray.strftime)
def strftime(self, date_format) -> Index:
arr = self._data.strftime(date_format)
return Index(arr, name=self.name)
return Index(arr, name=self.name, dtype=object)

@doc(DatetimeArray.tz_convert)
def tz_convert(self, tz) -> DatetimeIndex:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ def test_strftime_nat(self, data):
expected = Series(["2019-01-01", np.nan])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"data", [DatetimeIndex([pd.NaT]), PeriodIndex([pd.NaT], dtype="period[D]")]
)
def test_strftime_all_nat(self, data):
# https://github.com/pandas-dev/pandas/issues/45858
ser = Series(data)
with tm.assert_produces_warning(None):
result = ser.dt.strftime("%Y-%m-%d")
expected = Series([np.nan], dtype=object)
tm.assert_series_equal(result, expected)

def test_valid_dt_with_missing_values(self):

from datetime import (
Expand Down

0 comments on commit c14d3e1

Please sign in to comment.