Skip to content
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

REGR: fix bug in DatetimeIndex slicing with irregular or unsorted indices #37023

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f1d4a10
remove assert in core/generic.py and add test for datetimeindex slicing
CloseChoice Oct 10, 2020
cd1a136
fix formatting in test_generic
CloseChoice Oct 10, 2020
fe478c4
fix test and remove commented line
CloseChoice Oct 10, 2020
15a9185
updated tests according to PR comments
CloseChoice Oct 10, 2020
6caba47
slice with loc to prevent future warning
CloseChoice Oct 10, 2020
5703994
revert changes in io/formats/css.py
CloseChoice Oct 10, 2020
4a7ff03
run black on test_timeseries.py
CloseChoice Oct 10, 2020
25b229c
move tests to indexing/test_partial, add whatsnew entry
CloseChoice Oct 11, 2020
9bb10fe
reformat test_timeseries.py
CloseChoice Oct 11, 2020
9d82f83
Merge branch 'master' of https://github.com/pandas-dev/pandas into BU…
CloseChoice Oct 11, 2020
968fdf4
update whatsnew
CloseChoice Oct 11, 2020
89b0248
Merge branch 'master' into BUG-regression-datetimeindex-slicing
CloseChoice Oct 16, 2020
3710514
add suggestion from comment
CloseChoice Oct 16, 2020
237b085
use result and expected in tests
CloseChoice Oct 16, 2020
43a2a26
Merge branch 'master' of https://github.com/pandas-dev/pandas into BU…
CloseChoice Oct 20, 2020
632855b
Update doc/source/whatsnew/v1.1.4.rst
CloseChoice Oct 20, 2020
e79cdac
Merge branch 'master' of https://github.com/pandas-dev/pandas into BU…
CloseChoice Oct 21, 2020
e5f3615
Merge branch 'BUG-regression-datetimeindex-slicing' of github.com:Clo…
CloseChoice Oct 21, 2020
4261f02
Merge branch 'master' of https://github.com/pandas-dev/pandas into BU…
CloseChoice Oct 22, 2020
583003e
Merge remote-tracking branch 'upstream/master' into BUG-regression-da…
simonjayhawkins Oct 26, 2020
e246520
Update pandas/tests/indexing/test_partial.py
MarcoGorelli Oct 26, 2020
9cf919e
Update pandas/tests/indexing/test_partial.py
MarcoGorelli Oct 26, 2020
7f693dc
Update pandas/tests/indexing/test_partial.py
MarcoGorelli Oct 26, 2020
02ae223
Update pandas/tests/indexing/test_partial.py
MarcoGorelli Oct 26, 2020
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.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed regressions
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
- Fixed regression in :meth:`DataFrame.resample(...).apply(...)` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`)
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
- Fixed regression where slicing :class:`DatetimeIndex` failed on irregular time series with ``pd.NaT`` or on unsorted indices (:issue:`36953` and :issue:`35509`)
CloseChoice marked this conversation as resolved.
Show resolved Hide resolved

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,8 @@ def __getitem__(self, key):
# Do we have a slicer (on rows)?
indexer = convert_to_index_sliceable(self, key)
if indexer is not None:
if isinstance(indexer, np.ndarray):
indexer = lib.maybe_indices_to_slice(indexer, len(self))
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
return self._slice(indexer, axis=0)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,24 @@ def test_index_name_empty(self):
{"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index")
)
tm.assert_frame_equal(df, expected)

def test_slice_irregular_datetime_index_with_nan(self):
# GH36953
index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03", None])
df = pd.DataFrame(range(len(index)), index=index)
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
expected = pd.DataFrame(range(len(index[:3])), index=index[:3])
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
result = df["2012-01-01":"2012-01-04"]
tm.assert_frame_equal(result, expected)

def test_slice_datetime_index(self):
# GH35509
df = pd.DataFrame(
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
{"col1": ["a", "b", "c"], "col2": [1, 2, 3]},
index=pd.to_datetime(["2020-08-01", "2020-07-02", "2020-08-05"]),
)
expected = pd.DataFrame(
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
{"col1": ["a", "c"], "col2": [1, 3]},
index=pd.to_datetime(["2020-08-01", "2020-08-05"]),
)
result = df.loc["2020-08"]
tm.assert_frame_equal(result, expected)