Skip to content

Commit

Permalink
Fix pandas-nightly (#654)
Browse files Browse the repository at this point in the history
* Mostly fix pandas-nightly

* pd.PeriodDtype(freq=CustomBusinessDay)
  • Loading branch information
twoertwein authored Apr 20, 2023
1 parent f780a14 commit 3669013
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 77 deletions.
99 changes: 61 additions & 38 deletions tests/test_api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from pandas._typing import DtypeObj

from tests import check
from tests import (
check,
pytest_warns_bounded,
)

nparr = np.array([1, 2, 3])
arr = pd.Series([1, 2, 3])
Expand Down Expand Up @@ -52,15 +55,20 @@ def test_is_bool_dtype() -> None:


def test_is_categorical_dtype() -> None:
check(assert_type(api.is_categorical_dtype(arr), bool), bool)
check(assert_type(api.is_categorical_dtype(nparr), bool), bool)
check(assert_type(api.is_categorical_dtype(dtylike), bool), bool)
check(
assert_type(api.is_categorical_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_categorical_dtype(ind), bool), bool)
check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool)
with pytest_warns_bounded(
FutureWarning,
match="is_categorical_dtype is deprecated and will be removed in a future version",
lower="2.0.99",
):
check(assert_type(api.is_categorical_dtype(arr), bool), bool)
check(assert_type(api.is_categorical_dtype(nparr), bool), bool)
check(assert_type(api.is_categorical_dtype(dtylike), bool), bool)
check(
assert_type(api.is_categorical_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_categorical_dtype(ind), bool), bool)
check(assert_type(api.is_categorical_dtype(ExtensionDtype), bool), bool)


def test_is_complex() -> None:
Expand Down Expand Up @@ -124,15 +132,20 @@ def test_is_datetime64_ns_dtype() -> None:


def test_is_datetime64tz_dtype() -> None:
check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool)
check(
assert_type(api.is_datetime64tz_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool)
with pytest_warns_bounded(
FutureWarning,
match="is_datetime64tz_dtype is deprecated and will be removed in a future version",
lower="2.0.99",
):
check(assert_type(api.is_datetime64tz_dtype(arr), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(nparr), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(dtylike), bool), bool)
check(
assert_type(api.is_datetime64tz_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_datetime64tz_dtype(ind), bool), bool)
check(assert_type(api.is_datetime64tz_dtype(ExtensionDtype), bool), bool)


def test_is_dict_like() -> None:
Expand Down Expand Up @@ -209,15 +222,20 @@ def test_is_hashable() -> None:


def test_is_int64_dtype() -> None:
check(assert_type(api.is_int64_dtype(arr), bool), bool)
check(assert_type(api.is_int64_dtype(nparr), bool), bool)
check(assert_type(api.is_int64_dtype(dtylike), bool), bool)
check(
assert_type(api.is_int64_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_int64_dtype(ind), bool), bool)
# check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923
with pytest_warns_bounded(
FutureWarning,
match="is_int64_dtype is deprecated and will be removed in a future version",
lower="2.0.99",
):
check(assert_type(api.is_int64_dtype(arr), bool), bool)
check(assert_type(api.is_int64_dtype(nparr), bool), bool)
check(assert_type(api.is_int64_dtype(dtylike), bool), bool)
check(
assert_type(api.is_int64_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_int64_dtype(ind), bool), bool)
# check(assert_type(api.is_int64_dtype(ExtensionDtype), bool), bool) pandas GH 50923


def test_is_integer() -> None:
Expand Down Expand Up @@ -257,16 +275,21 @@ def test_is_interval() -> None:


def test_is_interval_dtype() -> None:
check(assert_type(api.is_interval_dtype(obj), bool), bool)
check(assert_type(api.is_interval_dtype(nparr), bool), bool)
check(assert_type(api.is_interval_dtype(dtylike), bool), bool)
check(assert_type(api.is_interval_dtype(arr), bool), bool)
check(
assert_type(api.is_interval_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_interval_dtype(ind), bool), bool)
check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool)
with pytest_warns_bounded(
FutureWarning,
match="is_interval_dtype is deprecated and will be removed in a future version",
lower="2.0.99",
):
check(assert_type(api.is_interval_dtype(obj), bool), bool)
check(assert_type(api.is_interval_dtype(nparr), bool), bool)
check(assert_type(api.is_interval_dtype(dtylike), bool), bool)
check(assert_type(api.is_interval_dtype(arr), bool), bool)
check(
assert_type(api.is_interval_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_interval_dtype(ind), bool), bool)
check(assert_type(api.is_interval_dtype(ExtensionDtype), bool), bool)


def test_is_iterator() -> None:
Expand Down
11 changes: 6 additions & 5 deletions tests/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
from pandas._libs.missing import NAType
from pandas._typing import Scalar

from tests import check
from tests import (
TYPE_CHECKING_INVALID_USAGE,
check,
)

from pandas.tseries.offsets import (
BusinessDay,
Expand Down Expand Up @@ -54,10 +57,8 @@ def test_period_dtype() -> None:
check(
assert_type(pd.PeriodDtype(freq=BusinessDay()), pd.PeriodDtype), pd.PeriodDtype
)
check(
assert_type(pd.PeriodDtype(freq=CustomBusinessDay()), pd.PeriodDtype),
pd.PeriodDtype,
)
if TYPE_CHECKING_INVALID_USAGE:
pd.PeriodDtype(freq=CustomBusinessDay()) # TODO(raises on 2.1)
check(
assert_type(p_dt.freq, pd.tseries.offsets.BaseOffset),
pd.tseries.offsets.DateOffset,
Expand Down
91 changes: 59 additions & 32 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
PD_LTE_20,
TYPE_CHECKING_INVALID_USAGE,
check,
pytest_warns_bounded,
)

from pandas.io.formats.style import Styler
Expand Down Expand Up @@ -726,12 +727,17 @@ def gethead(s: pd.Series, y: int) -> pd.Series:

def test_types_applymap() -> None:
df = pd.DataFrame(data={"col1": [2, 1], "col2": [3, 4]})
df.applymap(lambda x: x**2)
df.applymap(np.exp)
df.applymap(str)
# na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
df.applymap(np.exp, na_action="ignore")
df.applymap(str, na_action=None)
with pytest_warns_bounded(
FutureWarning,
match="DataFrame.applymap has been deprecated. Use DataFrame.map instead.",
lower="2.0.99",
):
df.applymap(lambda x: x**2)
df.applymap(np.exp)
df.applymap(str)
# na_action parameter was added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
df.applymap(np.exp, na_action="ignore")
df.applymap(str, na_action=None)


def test_types_element_wise_arithmetic() -> None:
Expand Down Expand Up @@ -875,7 +881,12 @@ def test_types_groupby() -> None:
df4: pd.DataFrame = df.groupby(by=["col1", "col2"]).count()
df5: pd.DataFrame = df.groupby(by=["col1", "col2"]).filter(lambda x: x["col1"] > 0)
df6: pd.DataFrame = df.groupby(by=["col1", "col2"]).nunique()
df7: pd.DataFrame = df.groupby(by="col1").apply(sum)
with pytest_warns_bounded(
FutureWarning,
match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated",
lower="2.0.99",
):
df7: pd.DataFrame = df.groupby(by="col1").apply(sum)
df8: pd.DataFrame = df.groupby("col1").transform("sum")
s1: pd.Series = df.set_index("col1")["col2"]
s2: pd.Series = s1.groupby("col1").transform("sum")
Expand Down Expand Up @@ -1454,9 +1465,19 @@ def test_types_regressions() -> None:

# https://github.com/microsoft/python-type-stubs/issues/115
df = pd.DataFrame({"A": [1, 2, 3], "B": [5, 6, 7]})
pd.DatetimeIndex(
data=df["A"], tz=None, normalize=False, closed=None, ambiguous="NaT", copy=True
)
with pytest_warns_bounded(
FutureWarning,
match="The 'closed' keyword in DatetimeIndex construction is deprecated",
lower="2.0.99",
):
pd.DatetimeIndex(
data=df["A"],
tz=None,
normalize=False,
closed=None,
ambiguous="NaT",
copy=True,
)


def test_read_csv() -> None:
Expand Down Expand Up @@ -2022,35 +2043,41 @@ def test_groupby_apply() -> None:
def sum_mean(x: pd.DataFrame) -> float:
return x.sum().mean()

check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)
with pytest_warns_bounded(
FutureWarning,
match="DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated",
lower="2.0.99",
):
check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)

lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
check(
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
pd.Series,
)
lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
check(
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
pd.Series,
)

def sum_to_list(x: pd.DataFrame) -> list:
return x.sum().tolist()
def sum_to_list(x: pd.DataFrame) -> list:
return x.sum().tolist()

check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)
check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)

def sum_to_series(x: pd.DataFrame) -> pd.Series:
return x.sum()
def sum_to_series(x: pd.DataFrame) -> pd.Series:
return x.sum()

check(
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame), pd.DataFrame
)
check(
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame),
pd.DataFrame,
)

def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
return x.sample()
def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
return x.sample()

check(
assert_type(
df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame
),
pd.DataFrame,
)
check(
assert_type(
df.groupby("col1", group_keys=False).apply(sample_to_df), pd.DataFrame
),
pd.DataFrame,
)


def test_resample() -> None:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,12 @@ def square(x: float) -> float:
def makeseries(x: float) -> pd.Series:
return pd.Series([x, 2 * x])

check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame)
with pytest_warns_bounded(
FutureWarning,
match="Returning a DataFrame from Series.apply when the supplied functionreturns a Series is deprecated",
lower="2.0.99",
):
check(assert_type(s.apply(makeseries), pd.DataFrame), pd.DataFrame)

# GH 293

Expand Down
10 changes: 9 additions & 1 deletion tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from pandas._libs.tslibs import BaseOffset
from pandas._libs.tslibs.offsets import DateOffset

from tests import pytest_warns_bounded

if TYPE_CHECKING:
from pandas._typing import FulldatetimeDict
else:
Expand Down Expand Up @@ -361,7 +363,13 @@ def test_series_dt_accessors() -> None:
check(assert_type(s0.dt.freq, Optional[str]), str)
check(assert_type(s0.dt.isocalendar(), pd.DataFrame), pd.DataFrame)
check(assert_type(s0.dt.to_period("D"), "PeriodSeries"), pd.Series, pd.Period)
check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime)

with pytest_warns_bounded(
FutureWarning,
match="The behavior of DatetimeProperties.to_pydatetime is deprecated",
lower="2.0.99",
):
check(assert_type(s0.dt.to_pydatetime(), np.ndarray), np.ndarray, dt.datetime)
s0_local = s0.dt.tz_localize("UTC")
check(
assert_type(s0_local, "TimestampSeries"),
Expand Down

0 comments on commit 3669013

Please sign in to comment.