File: _dataframe.py

package info (click to toggle)
scikit-learn 1.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 26,132 kB
  • sloc: python: 224,867; cpp: 5,790; ansic: 846; makefile: 190; javascript: 179
file content (123 lines) | stat: -rw-r--r-- 2,850 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Functions to determine if an object is a dataframe or series."""

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import sys


def is_df_or_series(X):
    """Return True if the X is a dataframe or series.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a dataframe or series, False otherwise.
    """
    return is_pandas_df_or_series(X) or is_polars_df_or_series(X) or is_pyarrow_data(X)


def is_pandas_df_or_series(X):
    """Return True if the X is a pandas dataframe or series.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a pandas dataframe or series, False otherwise.
    """
    try:
        pd = sys.modules["pandas"]
    except KeyError:
        return False
    return isinstance(X, (pd.DataFrame, pd.Series))


def is_pandas_df(X):
    """Return True if the X is a pandas dataframe.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a pandas dataframe, False otherwise.
    """
    try:
        pd = sys.modules["pandas"]
    except KeyError:
        return False
    return isinstance(X, pd.DataFrame)


def is_pyarrow_data(X):
    """Return True if the X is a pyarrow Table, RecordBatch, Array or ChunkedArray.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a pyarrow Table, RecordBatch, Array or ChunkedArray,
        False otherwise.
    """
    try:
        pa = sys.modules["pyarrow"]
    except KeyError:
        return False
    return isinstance(X, (pa.Table, pa.RecordBatch, pa.Array, pa.ChunkedArray))


def is_polars_df_or_series(X):
    """Return True if the X is a polars dataframe or series.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a polars dataframe or series, False otherwise.
    """
    try:
        pl = sys.modules["polars"]
    except KeyError:
        return False
    return isinstance(X, (pl.DataFrame, pl.Series))


def is_polars_df(X):
    """Return True if the X is a polars dataframe.

    Parameters
    ----------
    X : {array-like, dataframe}
        The array-like or dataframe object to check.

    Returns
    -------
    bool
        True if the X is a polarsdataframe, False otherwise.
    """
    try:
        pl = sys.modules["polars"]
    except KeyError:
        return False
    return isinstance(X, pl.DataFrame)