Skip to content

Commit 20d24b5

Browse files
committed
Merge remote-tracking branch 'upstream/master' into move-metadata-to-cfg
2 parents 012f859 + 0d5a644 commit 20d24b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+317
-251
lines changed

.pre-commit-config.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ repos:
150150
(?x)
151151
\#\ type:\ (?!ignore)
152152
|\#\ type:\s?ignore(?!\[)
153-
|\)\ ->\ \"
154153
language: pygrep
155154
types: [python]
156155
- id: np-bool
@@ -185,3 +184,7 @@ repos:
185184
- id: codespell
186185
types_or: [python, rst, markdown]
187186
files: ^pandas/core/
187+
- repo: https://github.com/MarcoGorelli/no-string-hints
188+
rev: v0.1.5
189+
hooks:
190+
- id: no-string-hints

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ Datetimelike
235235
- Bug in :meth:`DatetimeIndex.intersection`, :meth:`DatetimeIndex.symmetric_difference`, :meth:`PeriodIndex.intersection`, :meth:`PeriodIndex.symmetric_difference` always returning object-dtype when operating with :class:`CategoricalIndex` (:issue:`38741`)
236236
- Bug in :meth:`Series.where` incorrectly casting ``datetime64`` values to ``int64`` (:issue:`37682`)
237237
- Bug in :class:`Categorical` incorrectly typecasting ``datetime`` object to ``Timestamp`` (:issue:`38878`)
238+
- Bug in :func:`date_range` incorrectly creating :class:`DatetimeIndex` containing ``NaT`` instead of raising ``OutOfBoundsDatetime`` in corner cases (:issue:`24124`)
238239

239240
Timedelta
240241
^^^^^^^^^

pandas/core/aggregation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
kwarg aggregations in groupby and DataFrame/Series aggregation
44
"""
55

6+
from __future__ import annotations
7+
68
from collections import defaultdict
79
from functools import partial
810
from typing import (
@@ -296,7 +298,7 @@ def relabel_result(
296298
func: Dict[str, List[Union[Callable, str]]],
297299
columns: Iterable[Hashable],
298300
order: Iterable[int],
299-
) -> Dict[Hashable, "Series"]:
301+
) -> Dict[Hashable, Series]:
300302
"""
301303
Internal function to reorder result if relabelling is True for
302304
dataframe.agg, and return the reordered result in dict.
@@ -323,7 +325,7 @@ def relabel_result(
323325
reordered_indexes = [
324326
pair[0] for pair in sorted(zip(columns, order), key=lambda t: t[1])
325327
]
326-
reordered_result_in_dict: Dict[Hashable, "Series"] = {}
328+
reordered_result_in_dict: Dict[Hashable, Series] = {}
327329
idx = 0
328330

329331
reorder_mask = not isinstance(result, ABCSeries) and len(result.columns) > 1

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def factorize(
559559
sort: bool = False,
560560
na_sentinel: Optional[int] = -1,
561561
size_hint: Optional[int] = None,
562-
) -> Tuple[np.ndarray, Union[np.ndarray, "Index"]]:
562+
) -> Tuple[np.ndarray, Union[np.ndarray, Index]]:
563563
"""
564564
Encode the object as an enumerated type or categorical variable.
565565

pandas/core/arrays/_ranges.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import numpy as np
99

10-
from pandas._libs.tslibs import BaseOffset, OutOfBoundsDatetime, Timedelta, Timestamp
10+
from pandas._libs.tslibs import (
11+
BaseOffset,
12+
OutOfBoundsDatetime,
13+
Timedelta,
14+
Timestamp,
15+
iNaT,
16+
)
1117

1218

1319
def generate_regular_range(
@@ -150,7 +156,12 @@ def _generate_range_overflow_safe_signed(
150156
addend = np.int64(periods) * np.int64(stride)
151157
try:
152158
# easy case with no overflows
153-
return np.int64(endpoint) + addend
159+
result = np.int64(endpoint) + addend
160+
if result == iNaT:
161+
# Putting this into a DatetimeArray/TimedeltaArray
162+
# would incorrectly be interpreted as NaT
163+
raise OverflowError
164+
return result
154165
except (FloatingPointError, OverflowError):
155166
# with endpoint negative and addend positive we risk
156167
# FloatingPointError; with reversed signed we risk OverflowError

pandas/core/arrays/boolean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def numpy_dtype(self) -> np.dtype:
7272
return np.dtype("bool")
7373

7474
@classmethod
75-
def construct_array_type(cls) -> Type["BooleanArray"]:
75+
def construct_array_type(cls) -> Type[BooleanArray]:
7676
"""
7777
Return the array type associated with this dtype.
7878
@@ -94,7 +94,7 @@ def _is_numeric(self) -> bool:
9494
return True
9595

9696
def __from_arrow__(
97-
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
97+
self, array: Union[pyarrow.Array, pyarrow.ChunkedArray]
9898
) -> BooleanArray:
9999
"""
100100
Construct BooleanArray from pyarrow Array/ChunkedArray.

pandas/core/arrays/categorical.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def dtype(self) -> CategoricalDtype:
422422
return self._dtype
423423

424424
@property
425-
def _constructor(self) -> Type["Categorical"]:
425+
def _constructor(self) -> Type[Categorical]:
426426
return Categorical
427427

428428
@classmethod
@@ -2162,7 +2162,7 @@ def _concat_same_type(
21622162

21632163
# ------------------------------------------------------------------
21642164

2165-
def _encode_with_my_categories(self, other: "Categorical") -> Categorical:
2165+
def _encode_with_my_categories(self, other: Categorical) -> Categorical:
21662166
"""
21672167
Re-encode another categorical using this Categorical's categories.
21682168
@@ -2179,7 +2179,7 @@ def _encode_with_my_categories(self, other: "Categorical") -> Categorical:
21792179
)
21802180
return self._from_backing_data(codes)
21812181

2182-
def _categories_match_up_to_permutation(self, other: "Categorical") -> bool:
2182+
def _categories_match_up_to_permutation(self, other: Categorical) -> bool:
21832183
"""
21842184
Returns True if categoricals are the same dtype
21852185
same categories, and same ordered
@@ -2527,7 +2527,7 @@ def _delegate_method(self, name, *args, **kwargs):
25272527
# utility routines
25282528

25292529

2530-
def _get_codes_for_values(values, categories: "Index") -> np.ndarray:
2530+
def _get_codes_for_values(values, categories: Index) -> np.ndarray:
25312531
"""
25322532
utility routine to turn values into codes given the specified categories
25332533

pandas/core/arrays/floating.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _is_numeric(self) -> bool:
4747
return True
4848

4949
@classmethod
50-
def construct_array_type(cls) -> Type["FloatingArray"]:
50+
def construct_array_type(cls) -> Type[FloatingArray]:
5151
"""
5252
Return the array type associated with this dtype.
5353

pandas/core/arrays/integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _is_numeric(self) -> bool:
5757
return True
5858

5959
@classmethod
60-
def construct_array_type(cls) -> Type["IntegerArray"]:
60+
def construct_array_type(cls) -> Type[IntegerArray]:
6161
"""
6262
Return the array type associated with this dtype.
6363

pandas/core/arrays/numeric.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import numbers
35
from typing import TYPE_CHECKING, Any, List, Union
@@ -25,7 +27,7 @@
2527

2628
class NumericDtype(BaseMaskedDtype):
2729
def __from_arrow__(
28-
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
30+
self, array: Union[pyarrow.Array, pyarrow.ChunkedArray]
2931
) -> BaseMaskedArray:
3032
"""
3133
Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray.

pandas/core/arrays/numpy_.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def construct_from_string(cls, string: str) -> PandasDtype:
8989
return cls(dtype)
9090

9191
@classmethod
92-
def construct_array_type(cls) -> Type["PandasArray"]:
92+
def construct_array_type(cls) -> Type[PandasArray]:
9393
"""
9494
Return the array type associated with this dtype.
9595
@@ -155,7 +155,7 @@ class PandasArray(
155155
# ------------------------------------------------------------------------
156156
# Constructors
157157

158-
def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False):
158+
def __init__(self, values: Union[np.ndarray, PandasArray], copy: bool = False):
159159
if isinstance(values, type(self)):
160160
values = values._ndarray
161161
if not isinstance(values, np.ndarray):

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _simple_new(
201201

202202
@classmethod
203203
def _from_sequence(
204-
cls: Type["PeriodArray"],
204+
cls: Type[PeriodArray],
205205
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
206206
*,
207207
dtype: Optional[Dtype] = None,

pandas/core/arrays/sparse/array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
_sparray_doc_kwargs = {"klass": "SparseArray"}
6363

6464

65-
def _get_fill(arr: "SparseArray") -> np.ndarray:
65+
def _get_fill(arr: SparseArray) -> np.ndarray:
6666
"""
6767
Create a 0-dim ndarray containing the fill value
6868
@@ -87,7 +87,7 @@ def _get_fill(arr: "SparseArray") -> np.ndarray:
8787

8888

8989
def _sparse_array_op(
90-
left: "SparseArray", right: "SparseArray", op: Callable, name: str
90+
left: SparseArray, right: SparseArray, op: Callable, name: str
9191
) -> Any:
9292
"""
9393
Perform a binary operation between two arrays.
@@ -896,7 +896,7 @@ def _take_with_fill(self, indices, fill_value=None) -> np.ndarray:
896896

897897
return taken
898898

899-
def _take_without_fill(self, indices) -> Union[np.ndarray, "SparseArray"]:
899+
def _take_without_fill(self, indices) -> Union[np.ndarray, SparseArray]:
900900
to_shift = indices < 0
901901
indices = indices.copy()
902902

pandas/core/arrays/sparse/dtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def __repr__(self) -> str:
173173
return self.name
174174

175175
@classmethod
176-
def construct_array_type(cls) -> Type["SparseArray"]:
176+
def construct_array_type(cls) -> Type[SparseArray]:
177177
"""
178178
Return the array type associated with this dtype.
179179

pandas/core/arrays/string_.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def type(self) -> Type[str]:
7171
return str
7272

7373
@classmethod
74-
def construct_array_type(cls) -> Type["StringArray"]:
74+
def construct_array_type(cls) -> Type[StringArray]:
7575
"""
7676
Return the array type associated with this dtype.
7777
@@ -85,7 +85,7 @@ def __repr__(self) -> str:
8585
return "StringDtype"
8686

8787
def __from_arrow__(
88-
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
88+
self, array: Union[pyarrow.Array, pyarrow.ChunkedArray]
8989
) -> StringArray:
9090
"""
9191
Construct StringArray from pyarrow Array/ChunkedArray.

pandas/core/arrays/string_arrow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def type(self) -> Type[str]:
8787
return str
8888

8989
@classmethod
90-
def construct_array_type(cls) -> Type["ArrowStringArray"]:
90+
def construct_array_type(cls) -> Type[ArrowStringArray]:
9191
"""
9292
Return the array type associated with this dtype.
9393
@@ -104,7 +104,7 @@ def __repr__(self) -> str:
104104
return "ArrowStringDtype"
105105

106106
def __from_arrow__(
107-
self, array: Union["pa.Array", "pa.ChunkedArray"]
107+
self, array: Union[pa.Array, pa.ChunkedArray]
108108
) -> ArrowStringArray:
109109
"""
110110
Construct StringArray from pyarrow Array/ChunkedArray.

pandas/core/computation/ops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Operator classes for eval.
33
"""
44

5+
from __future__ import annotations
6+
57
from datetime import datetime
68
from distutils.version import LooseVersion
79
from functools import partial
@@ -203,7 +205,7 @@ class Op:
203205

204206
op: str
205207

206-
def __init__(self, op: str, operands: Iterable[Union[Term, "Op"]], encoding=None):
208+
def __init__(self, op: str, operands: Iterable[Union[Term, Op]], encoding=None):
207209
self.op = _bool_op_map.get(op, op)
208210
self.operands = operands
209211
self.encoding = encoding

pandas/core/describe.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class NDFrameDescriberAbstract(ABC):
9292
Whether to treat datetime dtypes as numeric.
9393
"""
9494

95-
def __init__(self, obj: "FrameOrSeriesUnion", datetime_is_numeric: bool):
95+
def __init__(self, obj: FrameOrSeriesUnion, datetime_is_numeric: bool):
9696
self.obj = obj
9797
self.datetime_is_numeric = datetime_is_numeric
9898

@@ -110,7 +110,7 @@ def describe(self, percentiles: Sequence[float]) -> FrameOrSeriesUnion:
110110
class SeriesDescriber(NDFrameDescriberAbstract):
111111
"""Class responsible for creating series description."""
112112

113-
obj: "Series"
113+
obj: Series
114114

115115
def describe(self, percentiles: Sequence[float]) -> Series:
116116
describe_func = select_describe_func(
@@ -137,7 +137,7 @@ class DataFrameDescriber(NDFrameDescriberAbstract):
137137

138138
def __init__(
139139
self,
140-
obj: "DataFrame",
140+
obj: DataFrame,
141141
*,
142142
include: Optional[Union[str, Sequence[str]]],
143143
exclude: Optional[Union[str, Sequence[str]]],
@@ -154,7 +154,7 @@ def __init__(
154154
def describe(self, percentiles: Sequence[float]) -> DataFrame:
155155
data = self._select_data()
156156

157-
ldesc: List["Series"] = []
157+
ldesc: List[Series] = []
158158
for _, series in data.items():
159159
describe_func = select_describe_func(series, self.datetime_is_numeric)
160160
ldesc.append(describe_func(series, percentiles))
@@ -191,7 +191,7 @@ def _select_data(self):
191191
return data
192192

193193

194-
def reorder_columns(ldesc: Sequence["Series"]) -> List[Hashable]:
194+
def reorder_columns(ldesc: Sequence[Series]) -> List[Hashable]:
195195
"""Set a convenient order for rows for display."""
196196
names: List[Hashable] = []
197197
ldesc_indexes = sorted((x.index for x in ldesc), key=len)
@@ -202,7 +202,7 @@ def reorder_columns(ldesc: Sequence["Series"]) -> List[Hashable]:
202202
return names
203203

204204

205-
def describe_numeric_1d(series: "Series", percentiles: Sequence[float]) -> Series:
205+
def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
206206
"""Describe series containing numerical data.
207207
208208
Parameters
@@ -226,7 +226,7 @@ def describe_numeric_1d(series: "Series", percentiles: Sequence[float]) -> Serie
226226

227227

228228
def describe_categorical_1d(
229-
data: "Series",
229+
data: Series,
230230
percentiles_ignored: Sequence[float],
231231
) -> Series:
232232
"""Describe series containing categorical data.
@@ -258,7 +258,7 @@ def describe_categorical_1d(
258258

259259

260260
def describe_timestamp_as_categorical_1d(
261-
data: "Series",
261+
data: Series,
262262
percentiles_ignored: Sequence[float],
263263
) -> Series:
264264
"""Describe series containing timestamp data treated as categorical.
@@ -305,7 +305,7 @@ def describe_timestamp_as_categorical_1d(
305305
return Series(result, index=names, name=data.name, dtype=dtype)
306306

307307

308-
def describe_timestamp_1d(data: "Series", percentiles: Sequence[float]) -> Series:
308+
def describe_timestamp_1d(data: Series, percentiles: Sequence[float]) -> Series:
309309
"""Describe series containing datetime64 dtype.
310310
311311
Parameters
@@ -330,7 +330,7 @@ def describe_timestamp_1d(data: "Series", percentiles: Sequence[float]) -> Serie
330330

331331

332332
def select_describe_func(
333-
data: "Series",
333+
data: Series,
334334
datetime_is_numeric: bool,
335335
) -> Callable:
336336
"""Select proper function for describing series based on data type.

0 commit comments

Comments
 (0)