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

Series(Mapping) #843

Merged
merged 6 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
cls,
data: DatetimeIndex
| Sequence[np.datetime64 | datetime]
| Mapping[Any, np.datetime64 | datetime]
twoertwein marked this conversation as resolved.
Show resolved Hide resolved
| np.datetime64
| datetime,
index: Axes | None = ...,
Expand All @@ -239,7 +240,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __new__( # type: ignore[overload-overlap]
cls,
data: _ListLike,
data: Iterable,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
index: Axes | None = ...,
*,
dtype: TimestampDtypeArg,
Expand All @@ -261,6 +262,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
cls,
data: TimedeltaIndex
| Sequence[np.timedelta64 | timedelta]
| Mapping[Any, np.timedelta64 | timedelta]
| np.timedelta64
| timedelta,
index: Axes | None = ...,
Expand All @@ -274,7 +276,8 @@ class Series(IndexOpsMixin[S1], NDFrame):
cls,
data: IntervalIndex[Interval[_OrderableT]]
| Interval[_OrderableT]
| Sequence[Interval[_OrderableT]],
| Sequence[Interval[_OrderableT]]
| Mapping[Any, Interval[_OrderableT]],
index: Axes | None = ...,
*,
dtype: Literal["Interval"] = ...,
Expand All @@ -284,7 +287,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __new__(
cls,
data: Scalar | _ListLike | dict[int, Any] | dict[_str, Any] | None,
data: Scalar | Iterable | None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think Iterable is correct here, because we don't (or shouldn't) accept a set.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it be correct to have one Never-overload for set and then simply use data: Any + specified dtype?

Copy link
Collaborator

@Dr-Irv Dr-Irv Dec 26, 2023

Choose a reason for hiding this comment

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

I don't know. I'd rather keep things narrow by just using Scalar | _ListLike | dict[Hashable, Any] | None . Or maybe we can use Mapping[Hashable, Scalar] instead of dict[Hashable, Any] .

My feeling is that we should make small increments when we change the stubs so as to not widen things too much.

Copy link
Member Author

Choose a reason for hiding this comment

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

My feeling is that we should make small increments when we change the stubs so as to not widen things too much.

[sorry, I edited your comment by accident - wanted to quote reply]

I agree about that! But I also feel like we are making ourselves too much work by trying to be as restrictive as possible.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree about that! But I also feel like we are making ourselves too much work by trying to be as restrictive as possible.

Possibly. But I'd rather let the community report issues, and we really haven't had too many issues where we made things too restrictive.

See what I wrote here 18 months ago about this topic: https://github.com/pandas-dev/pandas-stubs/blob/main/docs/philosophy.md#narrow-vs-wide-arguments

index: Axes | None = ...,
*,
dtype: type[S1],
Expand All @@ -294,7 +297,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __new__(
cls,
data: S1 | _ListLike[S1] | dict[int, S1] | dict[_str, S1],
data: S1 | Mapping[Any, S1] | _ListLike[S1],
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not happy with _ListLike but a Mapping is also Iterable.

index: Axes | None = ...,
*,
dtype: Dtype = ...,
Expand All @@ -304,7 +307,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __new__(
cls,
data: Scalar | _ListLike | dict[int, Any] | dict[_str, Any] | None = ...,
data: Scalar | Iterable | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as above about Iterable

index: Axes | None = ...,
*,
dtype: Dtype = ...,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2860,3 +2860,32 @@ def test_round() -> None:
def test_series_new_empty() -> None:
# GH 826
check(assert_type(pd.Series(), "pd.Series[Any]"), pd.Series)


def test_series_mapping() -> None:
# GH 831
check(
assert_type(
pd.Series(
{
pd.Timestamp(2023, 1, 2): "b",
}
),
"pd.Series[str]",
),
pd.Series,
str,
)

check(
assert_type(
pd.Series(
{
("a", "b"): "c",
}
),
"pd.Series[str]",
),
pd.Series,
str,
)