Skip to content

fix: #1304 add PD_LTE_23 #1306

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 4 commits into from
Aug 4, 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
44 changes: 34 additions & 10 deletions tests/series/arithmetic/test_truediv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import pandas as pd
from typing_extensions import assert_type

from tests import check
from tests import (
PD_LTE_23,
check,
)

left = pd.DataFrame({"a": [1, 2, 3]})["a"] # left operand

Expand Down Expand Up @@ -140,15 +143,36 @@ def test_truediv_pd_series() -> None:
check(assert_type(left.rdiv(c), pd.Series), pd.Series)


def test_truediv_path() -> None:
"""Test pd.Series / path object"""
left, p = pd.Series(["pat", "ath", "path"]), Path()
def test_truediv_paths(tmp_path: Path) -> None:
"""Test pd.Series of paths / path object.

check(assert_type(left / p, pd.Series), pd.Series, Path)
check(assert_type(p / left, pd.Series), pd.Series, Path)
Also GH 682."""
fpath = Path("a.png")
folders, fpaths = pd.Series([tmp_path, tmp_path]), pd.Series([fpath, fpath])

check(assert_type(left.truediv(p), pd.Series), pd.Series, Path)
check(assert_type(left.div(p), pd.Series), pd.Series, Path)
check(assert_type(folders / fpath, pd.Series), pd.Series, Path)
check(assert_type(folders.truediv(fpath), pd.Series), pd.Series, Path)
check(assert_type(folders.div(fpath), pd.Series), pd.Series, Path)

check(assert_type(left.rtruediv(p), pd.Series), pd.Series, Path)
check(assert_type(left.rdiv(p), pd.Series), pd.Series, Path)
# mypy thinks it's `Path`, in contrast to Series.__rtruediv__(self, other: Path) -> Series: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

I tried to come up with a simple example to illustrate the issue and couldn't.

Copy link
Member

Choose a reason for hiding this comment

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

Issue comes from how mypy reads the Path.__truediv__ typing and it prioritizes it over Series.rtruediv, not expert enough to know why

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I don't know why it did that. Tried some simple experiments and couldn't replicate it.

check(assert_type(tmp_path / fpaths, pd.Series), pd.Series, Path) # type: ignore[assert-type]
Copy link
Member

Choose a reason for hiding this comment

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

@Dr-Irv will confirm but I think this is an issue we have had in the past with mypy getting confused

check(assert_type(fpaths.rtruediv(tmp_path), pd.Series), pd.Series, Path)
check(assert_type(fpaths.rdiv(tmp_path), pd.Series), pd.Series, Path)


def test_truediv_path(tmp_path: Path) -> None:
"""Test pd.Series / path object.

Also GH 682."""
fnames = pd.Series(["a.png", "b.gz", "c.txt"])

if PD_LTE_23:
# Bug in 3.0 https://github.com/pandas-dev/pandas/issues/61940 (pyarrow.lib.ArrowInvalid)
check(assert_type(fnames / tmp_path, pd.Series), pd.Series, Path)
check(assert_type(tmp_path / fnames, pd.Series), pd.Series, Path)

check(assert_type(fnames.truediv(tmp_path), pd.Series), pd.Series, Path)
check(assert_type(fnames.div(tmp_path), pd.Series), pd.Series, Path)

check(assert_type(fnames.rtruediv(tmp_path), pd.Series), pd.Series, Path)
check(assert_type(fnames.rdiv(tmp_path), pd.Series), pd.Series, Path)
12 changes: 0 additions & 12 deletions tests/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3802,18 +3802,6 @@ def test_series_bool_fails() -> None:
pass


def test_path_div() -> None:
# GH 682
folder = Path.cwd()
files = pd.Series(["a.png", "b.png"])
if PD_LTE_23:
# Bug in 3.0 https://github.com/pandas-dev/pandas/issues/61940
check(assert_type(folder / files, pd.Series), pd.Series, Path)

folders = pd.Series([folder, folder])
check(assert_type(folders / Path("a.png"), pd.Series), pd.Series, Path)


def test_series_dict() -> None:
# GH 812
check(
Expand Down
Loading