@@ -61,6 +61,43 @@ The type `TimestampSeries` is the result of creating a series from `pd.to_dateti
61
61
the type ` TimedeltaSeries ` is the result of subtracting two ` TimestampSeries ` as well as
62
62
the result of ` pd.to_timedelta() ` .
63
63
64
+ ### Generic Series have restricted arithmetic
65
+
66
+ Consider the following Series from a DataFrame:
67
+
68
+ ``` python
69
+ import pandas as pd
70
+ from typing_extensions import reveal_type
71
+ from typing import TYPE_CHECKING , cast
72
+
73
+ if TYPE_CHECKING :
74
+ from pandas.core.series import TimestampSeries # noqa: F401
75
+
76
+
77
+ frame = pd.DataFrame({" timestamp" : [pd.Timestamp(2025 , 8 , 26 )], " tag" : [" one" ], " value" : [1.0 ]})
78
+ values = frame[" value" ]
79
+ reveal_type(values) # type checker: Series[Any], runtime: Series
80
+ new_values = values + 2
81
+
82
+ timestamps = frame[" timestamp" ]
83
+ reveal_type(timestamps) # type checker: Series[Any], runtime: Series
84
+ reveal_type(timestamps - pd.Timestamp(2025 , 7 , 12 )) # type checker: Unknown and error, runtime: Series
85
+ reveal_type(cast(" TimestampSeries" , timestamps) - pd.Timestamp(2025 , 7 , 12 )) # type checker: TimedeltaSeries, runtime: Series
86
+
87
+ tags = frame[" tag" ]
88
+ reveal_type(" suffix" + tags) # type checker: Never, runtime: Series
89
+ ```
90
+
91
+ Since they are taken from a DataFrame, all three of them, ` values ` , ` timestamps `
92
+ and ` tags ` , are recognized by type checkers as ` Series[Any] ` . The code snippet
93
+ runs fine at runtime. In the stub for type checking, however, we restrict
94
+ generic Series to perform arithmetic operations only with numeric types, and
95
+ give ` Series[Any] ` for the results. For ` Timedelta ` , ` Timestamp ` , ` str ` , etc.,
96
+ arithmetic is restricted to ` Series[Any] ` and the result is either undefined,
97
+ showing ` Unknown ` and errors, or ` Never ` . Users are encouraged to cast such
98
+ generic Series to ones with concrete types, so that type checkers can provide
99
+ meaningful results.
100
+
64
101
### Interval is Generic
65
102
66
103
A pandas ` Interval ` can be a time interval, an interval of integers, or an interval of
0 commit comments