|
15 | 15 | cast, |
16 | 16 | overload, |
17 | 17 | ) |
| 18 | +import warnings |
18 | 19 |
|
19 | 20 | import numpy as np |
20 | 21 |
|
|
25 | 26 | VALID_CLOSED, |
26 | 27 | Interval, |
27 | 28 | IntervalMixin, |
28 | | - _warning_interval, |
29 | 29 | intervals_to_interval_bounds, |
30 | 30 | ) |
31 | 31 | from pandas._libs.missing import NA |
|
43 | 43 | from pandas.errors import IntCastingNaNError |
44 | 44 | from pandas.util._decorators import ( |
45 | 45 | Appender, |
| 46 | + deprecate_kwarg, |
46 | 47 | deprecate_nonkeyword_arguments, |
47 | 48 | ) |
| 49 | +from pandas.util._exceptions import find_stack_level |
48 | 50 |
|
49 | 51 | from pandas.core.dtypes.cast import LossySetitemError |
50 | 52 | from pandas.core.dtypes.common import ( |
@@ -220,16 +222,15 @@ def ndim(self) -> Literal[1]: |
220 | 222 | # --------------------------------------------------------------------- |
221 | 223 | # Constructors |
222 | 224 |
|
| 225 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
223 | 226 | def __new__( |
224 | 227 | cls: type[IntervalArrayT], |
225 | 228 | data, |
226 | 229 | inclusive: str | None = None, |
227 | | - closed: None | lib.NoDefault = lib.no_default, |
228 | 230 | dtype: Dtype | None = None, |
229 | 231 | copy: bool = False, |
230 | 232 | verify_integrity: bool = True, |
231 | 233 | ): |
232 | | - inclusive, closed = _warning_interval(inclusive, closed) |
233 | 234 |
|
234 | 235 | data = extract_array(data, extract_numpy=True) |
235 | 236 |
|
@@ -267,24 +268,22 @@ def __new__( |
267 | 268 | ) |
268 | 269 |
|
269 | 270 | @classmethod |
| 271 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
270 | 272 | def _simple_new( |
271 | 273 | cls: type[IntervalArrayT], |
272 | 274 | left, |
273 | 275 | right, |
274 | 276 | inclusive=None, |
275 | | - closed: None | lib.NoDefault = lib.no_default, |
276 | 277 | copy: bool = False, |
277 | 278 | dtype: Dtype | None = None, |
278 | 279 | verify_integrity: bool = True, |
279 | 280 | ) -> IntervalArrayT: |
280 | 281 | result = IntervalMixin.__new__(cls) |
281 | 282 |
|
282 | | - inclusive, closed = _warning_interval(inclusive, closed) |
283 | | - |
284 | 283 | if inclusive is None and isinstance(dtype, IntervalDtype): |
285 | 284 | inclusive = dtype.inclusive |
286 | 285 |
|
287 | | - inclusive = inclusive or "both" |
| 286 | + inclusive = inclusive or "right" |
288 | 287 |
|
289 | 288 | left = ensure_index(left, copy=copy) |
290 | 289 | right = ensure_index(right, copy=copy) |
@@ -424,13 +423,17 @@ def _from_factorized( |
424 | 423 | ), |
425 | 424 | } |
426 | 425 | ) |
| 426 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
427 | 427 | def from_breaks( |
428 | 428 | cls: type[IntervalArrayT], |
429 | 429 | breaks, |
430 | | - inclusive="both", |
| 430 | + inclusive: IntervalClosedType | None = None, |
431 | 431 | copy: bool = False, |
432 | 432 | dtype: Dtype | None = None, |
433 | 433 | ) -> IntervalArrayT: |
| 434 | + if inclusive is None: |
| 435 | + inclusive = "right" |
| 436 | + |
434 | 437 | breaks = _maybe_convert_platform_interval(breaks) |
435 | 438 |
|
436 | 439 | return cls.from_arrays( |
@@ -501,14 +504,19 @@ def from_breaks( |
501 | 504 | ), |
502 | 505 | } |
503 | 506 | ) |
| 507 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
504 | 508 | def from_arrays( |
505 | 509 | cls: type[IntervalArrayT], |
506 | 510 | left, |
507 | 511 | right, |
508 | | - inclusive="both", |
| 512 | + inclusive: IntervalClosedType | None = None, |
509 | 513 | copy: bool = False, |
510 | 514 | dtype: Dtype | None = None, |
511 | 515 | ) -> IntervalArrayT: |
| 516 | + |
| 517 | + if inclusive is None: |
| 518 | + inclusive = "right" |
| 519 | + |
512 | 520 | left = _maybe_convert_platform_interval(left) |
513 | 521 | right = _maybe_convert_platform_interval(right) |
514 | 522 |
|
@@ -570,13 +578,17 @@ def from_arrays( |
570 | 578 | ), |
571 | 579 | } |
572 | 580 | ) |
| 581 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
573 | 582 | def from_tuples( |
574 | 583 | cls: type[IntervalArrayT], |
575 | 584 | data, |
576 | | - inclusive="both", |
| 585 | + inclusive=None, |
577 | 586 | copy: bool = False, |
578 | 587 | dtype: Dtype | None = None, |
579 | 588 | ) -> IntervalArrayT: |
| 589 | + if inclusive is None: |
| 590 | + inclusive = "right" |
| 591 | + |
580 | 592 | if len(data): |
581 | 593 | left, right = [], [] |
582 | 594 | else: |
@@ -1355,6 +1367,19 @@ def inclusive(self) -> IntervalClosedType: |
1355 | 1367 | """ |
1356 | 1368 | return self.dtype.inclusive |
1357 | 1369 |
|
| 1370 | + @property |
| 1371 | + def closed(self) -> IntervalClosedType: |
| 1372 | + """ |
| 1373 | + Whether the intervals are closed on the left-side, right-side, both or |
| 1374 | + neither. |
| 1375 | + """ |
| 1376 | + warnings.warn( |
| 1377 | + "Attribute `closed` is deprecated in favor of `inclusive`.", |
| 1378 | + FutureWarning, |
| 1379 | + stacklevel=find_stack_level(), |
| 1380 | + ) |
| 1381 | + return self.dtype.inclusive |
| 1382 | + |
1358 | 1383 | _interval_shared_docs["set_closed"] = textwrap.dedent( |
1359 | 1384 | """ |
1360 | 1385 | Return an %(klass)s identical to the current one, but closed on the |
@@ -1395,6 +1420,7 @@ def inclusive(self) -> IntervalClosedType: |
1395 | 1420 | ), |
1396 | 1421 | } |
1397 | 1422 | ) |
| 1423 | + @deprecate_kwarg(old_arg_name="closed", new_arg_name="inclusive") |
1398 | 1424 | def set_closed( |
1399 | 1425 | self: IntervalArrayT, inclusive: IntervalClosedType |
1400 | 1426 | ) -> IntervalArrayT: |
|
0 commit comments