Skip to content

BUG: DatetimeIndex.insert on empty can preserve freq #33573

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 3 commits into from
Apr 16, 2020
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/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ Indexing
- Bug in :meth:`DataFrame.copy` _item_cache not invalidated after copy causes post-copy value updates to not be reflected (:issue:`31784`)
- Bug in `Series.__getitem__` with an integer key and a :class:`MultiIndex` with leading integer level failing to raise ``KeyError`` if the key is not present in the first level (:issue:`33355`)
- Bug in :meth:`DataFrame.iloc` when slicing a single column-:class:`DataFrame`` with ``ExtensionDtype`` (e.g. ``df.iloc[:, :1]``) returning an invalid result (:issue:`32957`)
- Bug in :meth:`DatetimeIndex.insert` and :meth:`TimedeltaIndex.insert` causing index ``freq`` to be lost when setting an element into an empty :class:`Series` (:issue:33573`)

Missing
^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,10 @@ def insert(self, loc, item):
freq = self.freq
elif (loc == len(self)) and item - self.freq == self[-1]:
freq = self.freq
elif self.freq is not None:
# Adding a single item to an empty index may preserve freq
if self.freq.is_on_offset(item):
freq = self.freq
item = item.asm8

try:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/datetimes/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def test_insert_invalid_na(self, tz):
with pytest.raises(TypeError, match="incompatible label"):
idx.insert(0, np.timedelta64("NaT"))

def test_insert_empty_preserves_freq(self, tz_naive_fixture):
# GH#33573
tz = tz_naive_fixture
dti = DatetimeIndex([], tz=tz, freq="D")
item = Timestamp("2017-04-05").tz_localize(tz)

result = dti.insert(0, item)
assert result.freq == dti.freq

# But not when we insert an item that doesnt conform to freq
dti = DatetimeIndex([], tz=tz, freq="W-THU")
result = dti.insert(0, item)
assert result.freq is None

def test_insert(self):
idx = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"], name="idx")

Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/indexes/timedeltas/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ def test_insert_dont_cast_strings(self):

def test_insert_empty(self):
# Corner case inserting with length zero doesnt raise IndexError
# GH#33573 for freq preservation
idx = timedelta_range("1 Day", periods=3)
td = idx[0]

idx[:0].insert(0, td)
idx[:0].insert(1, td)
idx[:0].insert(-1, td)
result = idx[:0].insert(0, td)
assert result.freq == "D"

result = idx[:0].insert(1, td)
assert result.freq == "D"

result = idx[:0].insert(-1, td)
assert result.freq == "D"
4 changes: 4 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,21 @@ def test_setitem(datetime_series, string_series):
expected = string_series.append(app)
tm.assert_series_equal(s, expected)


def test_setitem_empty_series():
# Test for issue #10193
key = pd.Timestamp("2012-01-01")
series = pd.Series(dtype=object)
series[key] = 47
expected = pd.Series(47, [key])
tm.assert_series_equal(series, expected)

# GH#33573 our index should retain its freq
series = pd.Series([], pd.DatetimeIndex([], freq="D"), dtype=object)
series[key] = 47
expected = pd.Series(47, pd.DatetimeIndex([key], freq="D"))
tm.assert_series_equal(series, expected)
assert series.index.freq == expected.index.freq


def test_setitem_dtypes():
Expand Down