_typing.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from pathlib import Path
  2. from typing import (
  3. IO,
  4. TYPE_CHECKING,
  5. Any,
  6. AnyStr,
  7. Callable,
  8. Collection,
  9. Dict,
  10. Hashable,
  11. List,
  12. Mapping,
  13. Optional,
  14. TypeVar,
  15. Union,
  16. )
  17. import numpy as np
  18. # To prevent import cycles place any internal imports in the branch below
  19. # and use a string literal forward reference to it in subsequent types
  20. # https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
  21. if TYPE_CHECKING:
  22. from pandas._libs import Period, Timedelta, Timestamp # noqa: F401
  23. from pandas.core.arrays.base import ExtensionArray # noqa: F401
  24. from pandas.core.dtypes.dtypes import ExtensionDtype # noqa: F401
  25. from pandas.core.indexes.base import Index # noqa: F401
  26. from pandas.core.generic import NDFrame # noqa: F401
  27. from pandas import Interval # noqa: F401
  28. from pandas.core.series import Series # noqa: F401
  29. from pandas.core.frame import DataFrame # noqa: F401
  30. # array-like
  31. AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
  32. ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray)
  33. # scalars
  34. PythonScalar = Union[str, int, float, bool]
  35. DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta")
  36. PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]
  37. Scalar = Union[PythonScalar, PandasScalar]
  38. # other
  39. Dtype = Union[str, np.dtype, "ExtensionDtype"]
  40. FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
  41. # FrameOrSeriesUnion means either a DataFrame or a Series. E.g.
  42. # `def func(a: FrameOrSeriesUnion) -> FrameOrSeriesUnion: ...` means that if a Series
  43. # is passed in, either a Series or DataFrame is returned, and if a DataFrame is passed
  44. # in, either a DataFrame or a Series is returned.
  45. FrameOrSeriesUnion = Union["DataFrame", "Series"]
  46. # FrameOrSeries is stricter and ensures that the same subclass of NDFrame always is
  47. # used. E.g. `def func(a: FrameOrSeries) -> FrameOrSeries: ...` means that if a
  48. # Series is passed into a function, a Series is always returned and if a DataFrame is
  49. # passed in, a DataFrame is always returned.
  50. FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame")
  51. Axis = Union[str, int]
  52. Label = Optional[Hashable]
  53. Level = Union[Label, int]
  54. Ordered = Optional[bool]
  55. JSONSerializable = Union[PythonScalar, List, Dict]
  56. Axes = Collection
  57. # For functions like rename that convert one label to another
  58. Renamer = Union[Mapping[Label, Any], Callable[[Label], Label]]
  59. # to maintain type information across generic functions and parametrization
  60. T = TypeVar("T")