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

GH1089 Fix pandas nightly and migrate tests to new framework #1088

Merged
merged 2 commits into from
Jan 9, 2025
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
5 changes: 5 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,7 @@ def sum_mean(x: pd.DataFrame) -> float:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.apply operated on the grouping columns.",
upper="2.99",
):
check(
assert_type(df.groupby("col1").apply(sum_mean), pd.Series),
Expand All @@ -2972,6 +2973,7 @@ def sum_mean(x: pd.DataFrame) -> float:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.apply operated on the grouping columns.",
upper="2.99",
):
check(assert_type(df.groupby("col1").apply(lfunc), pd.Series), pd.Series)

Expand All @@ -2981,6 +2983,7 @@ def sum_to_list(x: pd.DataFrame) -> list:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.apply operated on the grouping columns.",
upper="2.99",
):
check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)

Expand All @@ -2990,6 +2993,7 @@ def sum_to_series(x: pd.DataFrame) -> pd.Series:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.apply operated on the grouping columns.",
upper="2.99",
):
check(
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame),
Expand All @@ -3002,6 +3006,7 @@ def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.apply operated on the grouping columns.",
upper="2.99",
):
check(
assert_type(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_frame_groupby_resample() -> None:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
upper="2.99",
):
check(assert_type(GB_DF.resample("ME").sum(), DataFrame), DataFrame)
check(assert_type(GB_DF.resample("ME").prod(), DataFrame), DataFrame)
Expand Down Expand Up @@ -126,6 +127,7 @@ def test_frame_groupby_resample() -> None:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
upper="2.99",
):
with pytest_warns_bounded(
FutureWarning,
Expand Down Expand Up @@ -173,6 +175,7 @@ def f(val: DataFrame) -> Series:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
upper="2.99",
):
check(assert_type(GB_DF.resample("ME").aggregate(f), DataFrame), DataFrame)

Expand All @@ -189,6 +192,7 @@ def df2scalar(val: DataFrame) -> float:
with pytest_warns_bounded(
DeprecationWarning,
"DataFrameGroupBy.(apply|resample) operated on the grouping columns",
upper="2.99",
):
with pytest_warns_bounded(
FutureWarning,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ def test_column_contains() -> None:
df = pd.DataFrame({"A": [1, 2], "B": ["c", "d"], "E": [3, 4]})

collist = [column for column in df.columns]
check(assert_type(collist, list[str]), list, str)

collist2 = [column for column in df.columns[df.columns.str.contains("A|B")]]
check(assert_type(collist2, list[str]), list, str)

length = len(df.columns[df.columns.str.contains("A|B")])
check(assert_type(length, int), int)


def test_column_sequence() -> None:
Expand Down
61 changes: 47 additions & 14 deletions tests/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,61 @@


def test_interval_init() -> None:
i1: pd.Interval = pd.Interval(1, 2, closed="both")
i2: pd.Interval = pd.Interval(1, right=2, closed="right")
i3: pd.Interval = pd.Interval(left=1, right=2, closed="left")
check(
assert_type(pd.Interval(1, 2, closed="both"), "pd.Interval[int]"),
pd.Interval,
int,
)
check(
assert_type(pd.Interval(1, right=2, closed="right"), "pd.Interval[int]"),
pd.Interval,
int,
)
check(
assert_type(pd.Interval(left=1, right=2, closed="left"), "pd.Interval[int]"),
pd.Interval,
int,
)


def test_interval_arithmetic() -> None:
i1: pd.Interval = pd.Interval(1, 2, closed="both")
i2: pd.Interval = pd.Interval(1, right=2, closed="right")
i1 = pd.Interval(1, 2, closed="both")
check(assert_type(i1, "pd.Interval[int]"), pd.Interval, int)
check(
assert_type(pd.Interval(1, right=2, closed="right"), "pd.Interval[int]"),
pd.Interval,
int,
)

i3: pd.Interval = i1 + 1
i4: pd.Interval = i1 - 1
i5: pd.Interval = i1 * 2
i6: pd.Interval = i1 / 2
i7: pd.Interval = i1 // 2
check(assert_type(i1 + 1, "pd.Interval[int]"), pd.Interval, int)
check(assert_type(i1 - 1, "pd.Interval[int]"), pd.Interval, int)
check(assert_type(i1 * 2, "pd.Interval[int]"), pd.Interval, int)
check(assert_type(i1 / 2, "pd.Interval[float]"), pd.Interval, float)
check(assert_type(i1 // 2, "pd.Interval[int]"), pd.Interval, int)


def test_max_intervals() -> None:
i1 = pd.Interval(
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02"), closed="both"
check(
assert_type(
pd.Interval(
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02"), closed="both"
),
"pd.Interval[pd.Timestamp]",
),
pd.Interval,
pd.Timestamp,
)
i2 = pd.Interval(
pd.Timestamp("2000-01-01T12:00:00"), pd.Timestamp("2000-01-02"), closed="both"
check(
assert_type(
pd.Interval(
pd.Timestamp("2000-01-01T12:00:00"),
pd.Timestamp("2000-01-02"),
closed="both",
),
"pd.Interval[pd.Timestamp]",
),
pd.Interval,
pd.Timestamp,
)


Expand Down
58 changes: 47 additions & 11 deletions tests/test_interval_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,63 @@


def test_from_breaks() -> None:
ind1: pd.IntervalIndex = pd.IntervalIndex.from_breaks([0, 1, 2, 3], name="test")
ind2: pd.IntervalIndex = pd.IntervalIndex.from_breaks(
[0, 1, 2, 3], closed="right", name=123
check(
assert_type(
pd.IntervalIndex.from_breaks([0, 1, 2, 3], name="test"),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)
check(
assert_type(
pd.IntervalIndex.from_breaks([0, 1, 2, 3], closed="right", name=123),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)


def test_from_arrays() -> None:
ind1: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
[0, 1, 2], [1, 2, 3], name="test"
check(
assert_type(
pd.IntervalIndex.from_arrays([0, 1, 2], [1, 2, 3], name="test"),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)
ind2: pd.IntervalIndex = pd.IntervalIndex.from_arrays(
[0, 1, 2], [1, 2, 3], closed="right", name=123
check(
assert_type(
pd.IntervalIndex.from_arrays(
[0, 1, 2], [1, 2, 3], closed="right", name=123
),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)


def test_from_tuples() -> None:
ind1: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
[(0, 1), (1, 2), (2, 3)], name="test"
check(
assert_type(
pd.IntervalIndex.from_tuples([(0, 1), (1, 2), (2, 3)], name="test"),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)
ind2: pd.IntervalIndex = pd.IntervalIndex.from_tuples(
[(0, 1), (1, 2), (2, 3)], closed="right", name=123
check(
assert_type(
pd.IntervalIndex.from_tuples(
[(0, 1), (1, 2), (2, 3)], closed="right", name=123
),
"pd.IntervalIndex[pd.Interval[int]]",
),
pd.IntervalIndex,
pd.Interval,
)


Expand Down
98 changes: 80 additions & 18 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest
from typing_extensions import (
Never,
TypeAlias,
assert_type,
)

Expand All @@ -33,23 +34,66 @@
pytest_warns_bounded,
)

if TYPE_CHECKING:
from pandas.core.series import TimestampSeries
else:
TimestampSeries: TypeAlias = pd.Series


def test_types_to_datetime() -> None:
df = pd.DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]})
r1: pd.Series = pd.to_datetime(df)
check(assert_type(pd.to_datetime(df), TimestampSeries), pd.Series, pd.Timestamp)

r2: pd.Series = pd.to_datetime(df, unit="s", origin="unix")
r3: pd.Series = pd.to_datetime(
df, unit="ns", dayfirst=True, utc=False, format="%M:%D", exact=False
check(
assert_type(pd.to_datetime(df, unit="s", origin="unix"), TimestampSeries),
pd.Series,
pd.Timestamp,
)
check(
assert_type(
pd.to_datetime(
df, unit="ns", dayfirst=True, utc=False, format="%M:%D", exact=False
),
TimestampSeries,
),
pd.Series,
pd.Timestamp,
)
check(
assert_type(
pd.to_datetime([1, 2], unit="D", origin=pd.Timestamp("01/01/2000")),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
check(
assert_type(
pd.to_datetime([1, 2], unit="D", origin=3),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
check(
assert_type(
pd.to_datetime(["2022-01-03", "2022-02-22"]),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
r4: pd.DatetimeIndex = pd.to_datetime(
[1, 2], unit="D", origin=pd.Timestamp("01/01/2000")
check(
assert_type(
pd.to_datetime(pd.Index(["2022-01-03", "2022-02-22"])),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
r5: pd.DatetimeIndex = pd.to_datetime([1, 2], unit="D", origin=3)
r6: pd.DatetimeIndex = pd.to_datetime(["2022-01-03", "2022-02-22"])
r7: pd.DatetimeIndex = pd.to_datetime(pd.Index(["2022-01-03", "2022-02-22"]))
r8: pd.Series = pd.to_datetime(
{"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}
check(
assert_type(
pd.to_datetime({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}),
TimestampSeries,
),
pd.Series,
pd.Timestamp,
)


Expand Down Expand Up @@ -179,7 +223,7 @@ def test_types_concat() -> None:

check(
assert_type(
pd.concat(map(lambda x: s2, ["some_value", 3]), axis=1), pd.DataFrame
pd.concat(map(lambda _: s2, ["some_value", 3]), axis=1), pd.DataFrame
),
pd.DataFrame,
)
Expand Down Expand Up @@ -306,14 +350,32 @@ def test_types_json_normalize() -> None:
{"name": {"given": "Mose", "family": "Regner"}},
{"id": 2, "name": "Faye Raker"},
]
df1: pd.DataFrame = pd.json_normalize(data=data1)
df2: pd.DataFrame = pd.json_normalize(data=data1, max_level=0, sep=";")
df3: pd.DataFrame = pd.json_normalize(
data=data1, meta_prefix="id", record_prefix="name", errors="raise"
check(assert_type(pd.json_normalize(data=data1), pd.DataFrame), pd.DataFrame)
check(
assert_type(pd.json_normalize(data=data1, max_level=0, sep=";"), pd.DataFrame),
pd.DataFrame,
)
check(
assert_type(
pd.json_normalize(
data=data1, meta_prefix="id", record_prefix="name", errors="raise"
),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
pd.json_normalize(data=data1, record_path=None, meta="id"), pd.DataFrame
),
pd.DataFrame,
)
df4: pd.DataFrame = pd.json_normalize(data=data1, record_path=None, meta="id")
data2: dict[str, Any] = {"name": {"given": "Mose", "family": "Regner"}}
df5: pd.DataFrame = pd.json_normalize(data=data2)
check(
assert_type(data2, dict[str, Any]),
dict,
)
check(assert_type(pd.json_normalize(data=data2), pd.DataFrame), pd.DataFrame)


def test_isna() -> None:
Expand Down
Loading
Loading