|
4 | 4 |
|
5 | 5 | These should not depend on core.internals.
|
6 | 6 | """
|
7 |
| -from typing import Optional, Sequence, Union, cast |
| 7 | +from typing import TYPE_CHECKING, Any, Optional, Sequence, Union, cast |
8 | 8 |
|
9 | 9 | import numpy as np
|
10 | 10 | import numpy.ma as ma
|
|
44 | 44 | )
|
45 | 45 | from pandas.core.dtypes.missing import isna
|
46 | 46 |
|
| 47 | +from pandas._typing import ArrayLike, Dtype |
47 | 48 | import pandas.core.common as com
|
48 | 49 |
|
| 50 | +if TYPE_CHECKING: |
| 51 | + from pandas.core.series import Series # noqa: F401 |
| 52 | + from pandas.core.index import Index # noqa: F401 |
| 53 | + |
49 | 54 |
|
50 | 55 | def array(
|
51 | 56 | data: Sequence[object],
|
@@ -565,3 +570,62 @@ def _try_cast(
|
565 | 570 | else:
|
566 | 571 | subarr = np.array(arr, dtype=object, copy=copy)
|
567 | 572 | return subarr
|
| 573 | + |
| 574 | + |
| 575 | +def is_empty_data(data: Any) -> bool: |
| 576 | + """ |
| 577 | + Utility to check if a Series is instantiated with empty data, |
| 578 | + which does not contain dtype information. |
| 579 | +
|
| 580 | + Parameters |
| 581 | + ---------- |
| 582 | + data : array-like, Iterable, dict, or scalar value |
| 583 | + Contains data stored in Series. |
| 584 | +
|
| 585 | + Returns |
| 586 | + ------- |
| 587 | + bool |
| 588 | + """ |
| 589 | + is_none = data is None |
| 590 | + is_list_like_without_dtype = is_list_like(data) and not hasattr(data, "dtype") |
| 591 | + is_simple_empty = is_list_like_without_dtype and not data |
| 592 | + return is_none or is_simple_empty |
| 593 | + |
| 594 | + |
| 595 | +def create_series_with_explicit_dtype( |
| 596 | + data: Any = None, |
| 597 | + index: Optional[Union[ArrayLike, "Index"]] = None, |
| 598 | + dtype: Optional[Dtype] = None, |
| 599 | + name: Optional[str] = None, |
| 600 | + copy: bool = False, |
| 601 | + fastpath: bool = False, |
| 602 | + dtype_if_empty: Dtype = object, |
| 603 | +) -> "Series": |
| 604 | + """ |
| 605 | + Helper to pass an explicit dtype when instantiating an empty Series. |
| 606 | +
|
| 607 | + This silences a DeprecationWarning described in GitHub-17261. |
| 608 | +
|
| 609 | + Parameters |
| 610 | + ---------- |
| 611 | + data : Mirrored from Series.__init__ |
| 612 | + index : Mirrored from Series.__init__ |
| 613 | + dtype : Mirrored from Series.__init__ |
| 614 | + name : Mirrored from Series.__init__ |
| 615 | + copy : Mirrored from Series.__init__ |
| 616 | + fastpath : Mirrored from Series.__init__ |
| 617 | + dtype_if_empty : str, numpy.dtype, or ExtensionDtype |
| 618 | + This dtype will be passed explicitly if an empty Series will |
| 619 | + be instantiated. |
| 620 | +
|
| 621 | + Returns |
| 622 | + ------- |
| 623 | + Series |
| 624 | + """ |
| 625 | + from pandas.core.series import Series |
| 626 | + |
| 627 | + if is_empty_data(data) and dtype is None: |
| 628 | + dtype = dtype_if_empty |
| 629 | + return Series( |
| 630 | + data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath |
| 631 | + ) |
0 commit comments