Skip to content

FIX: query() and isin() fail on period type column #54214

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
na_value_for_dtype,
)

from pandas.core.api import PeriodDtype
from pandas.core.array_algos.take import take_nd
from pandas.core.construction import (
array as pd_array,
Expand Down Expand Up @@ -501,6 +502,8 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
comps_array = extract_array(comps_array, extract_numpy=True)
if not isinstance(comps_array, np.ndarray):
# i.e. Extension Array
if isinstance(comps_array.dtype, PeriodDtype):
comps_array = comps_array.to_timestamp()
return comps_array.isin(values)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 testcases fail post this change, looking into it

FAILED pandas/tests/indexes/test_datetimelike.py::TestDatetimeLike::test_isin[simple_index0] - TypeError: Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead
FAILED pandas/tests/series/methods/test_isin.py::TestSeriesIsIn::test_isin_period_freq_mismatch - TypeError: Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead

elif needs_i8_conversion(comps_array.dtype):
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arrays/categorical/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ def test_isin_empty(empty):
tm.assert_numpy_array_equal(expected, result)


def test_isin_period_range():
period_range = pd.period_range("2000-01-01", periods=3)
element = ["2000-01-02"]
result = period_range.isin(element)
expected = np.array([False, True, False], dtype=bool)
tm.assert_numpy_array_equal(expected, result)


def test_isin_date_dtype():
period_range = pd.date_range("2000-01-01", periods=3)
element = ["2000-01-02"]
result = period_range.isin(element)
expected = np.array([False, True, False], dtype=bool)
tm.assert_numpy_array_equal(expected, result)


def test_diff():
ser = pd.Series([1, 2, 3], dtype="category")

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MultiIndex,
Series,
date_range,
period_range,
)
import pandas._testing as tm
from pandas.core.computation.check import NUMEXPR_INSTALLED
Expand Down Expand Up @@ -1404,3 +1405,21 @@ def test_query_ea_equality_comparison(self, dtype, engine):
}
)
tm.assert_frame_equal(result, expected)


class TestPeriodDtypeQueryPythonPandas(TestDataFrameQueryNumExprPandas):
@pytest.fixture
def engine(self):
return "python"

@pytest.fixture
def parser(self):
return "pandas"

def test_query_builtin(self, engine, parser):
df = DataFrame(
{"Date": period_range("2000-01-01", periods=10, name="Date")},
)
result = df.query("Date == '2000-01-06'", parser=parser, engine=engine)
expected = DataFrame([{"Date": pd.Period("2000-01-06", "D")}], index=[5])
tm.assert_frame_equal(result, expected)