Repository: pandas-dev/pandas-stubs (language: python)

Issue:
dict argument to `pd.Series` constructor should allow `Timestamp` and `tuple` as keys

Additional migration issues for the `pd.Series` constructor involve passing a dictionary.  It fails with a Timestamp or multi-index key whereas before it did not:

```
sr1 = pd.Series(
        {
            pd.Timestamp(2023, 1, 2): "b",
        }
    )

    sr2 = pd.Series(
        {
            ("a", "b"): "c",
        }
    )
```

```
test.py:172: error: Need type annotation for "sr1"  [var-annotated]
test.py:174: error: Dict entry 0 has incompatible type "Timestamp": "str"; expected "str": "ndarray[Any, Any]"  [dict-item]
test.py:178: error: Need type annotation for "sr2"  [var-annotated]
test.py:180: error: Dict entry 0 has incompatible type "Tuple[str, str]": "str"; expected "str": "ndarray[Any, Any]"  [dict-item]
```

_Originally posted by @amgcc in https://github.com/pandas-dev/pandas-stubs/issues/826#issuecomment-1858252851_


Might be best to allow `Hashable` as the `dict` key.
            


Discussion / hints:

Fix patch:
diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi
index 40738979..c40f5820 100644
--- a/pandas-stubs/core/series.pyi
+++ b/pandas-stubs/core/series.pyi
@@ -228,6 +228,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
         cls,
         data: DatetimeIndex
         | Sequence[np.datetime64 | datetime]
+        | dict[HashableT1, np.datetime64 | datetime]
         | np.datetime64
         | datetime,
         index: Axes | None = ...,
@@ -261,6 +262,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
         cls,
         data: TimedeltaIndex
         | Sequence[np.timedelta64 | timedelta]
+        | dict[HashableT1, np.timedelta64 | timedelta]
         | np.timedelta64
         | timedelta,
         index: Axes | None = ...,
@@ -274,7 +276,8 @@ class Series(IndexOpsMixin[S1], NDFrame):
         cls,
         data: IntervalIndex[Interval[_OrderableT]]
         | Interval[_OrderableT]
-        | Sequence[Interval[_OrderableT]],
+        | Sequence[Interval[_OrderableT]]
+        | dict[HashableT1, Interval[_OrderableT]],
         index: Axes | None = ...,
         *,
         dtype: Literal["Interval"] = ...,
@@ -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 | _ListLike | dict[HashableT1, Any] | None,
         index: Axes | None = ...,
         *,
         dtype: type[S1],
@@ -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 | _ListLike[S1] | dict[HashableT1, S1],
         index: Axes | None = ...,
         *,
         dtype: Dtype = ...,
@@ -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 | _ListLike | dict[HashableT1, Any] | None = ...,
         index: Axes | None = ...,
         *,
         dtype: Dtype = ...,


Test patch:
diff --git a/tests/test_series.py b/tests/test_series.py
index cf376221..f93a4a8f 100644
--- a/tests/test_series.py
+++ b/tests/test_series.py
@@ -2863,6 +2863,35 @@ def test_series_new_empty() -> None:
     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,
+    )
+
+
 def test_timedeltaseries_operators() -> None:
     series = pd.Series([pd.Timedelta(days=1)])
     check(