-
Notifications
You must be signed in to change notification settings - Fork 647
/
utils.py
201 lines (161 loc) · 5.8 KB
/
utils.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
"""Implement utils for pandas component."""
from pandas import MultiIndex
from modin.utils import hashable
def from_non_pandas(df, index, columns, dtype):
"""
Convert a non-pandas DataFrame into Modin DataFrame.
Parameters
----------
df : object
Non-pandas DataFrame.
index : object
Index for non-pandas DataFrame.
columns : object
Columns for non-pandas DataFrame.
dtype : type
Data type to force.
Returns
-------
modin.pandas.DataFrame
Converted DataFrame.
"""
from modin.core.execution.dispatching.factories.dispatcher import FactoryDispatcher
new_qc = FactoryDispatcher.from_non_pandas(df, index, columns, dtype)
if new_qc is not None:
from .dataframe import DataFrame
return DataFrame(query_compiler=new_qc)
return new_qc
def from_pandas(df):
"""
Convert a pandas DataFrame to a Modin DataFrame.
Parameters
----------
df : pandas.DataFrame
The pandas DataFrame to convert.
Returns
-------
modin.pandas.DataFrame
A new Modin DataFrame object.
"""
from modin.core.execution.dispatching.factories.dispatcher import FactoryDispatcher
from .dataframe import DataFrame
return DataFrame(query_compiler=FactoryDispatcher.from_pandas(df))
def from_arrow(at):
"""
Convert an Arrow Table to a Modin DataFrame.
Parameters
----------
at : Arrow Table
The Arrow Table to convert from.
Returns
-------
DataFrame
A new Modin DataFrame object.
"""
from modin.core.execution.dispatching.factories.dispatcher import FactoryDispatcher
from .dataframe import DataFrame
return DataFrame(query_compiler=FactoryDispatcher.from_arrow(at))
def is_scalar(obj):
"""
Return True if given object is scalar.
This method works the same as is_scalar method from pandas but
it is optimized for Modin frames. For BasePandasDataset objects
pandas version of is_scalar tries to access missing attribute
causing index scan. This triggers execution for lazy frames and
we avoid it by handling BasePandasDataset objects separately.
Parameters
----------
obj : object
Object to check.
Returns
-------
bool
True if given object is scalar and False otherwise.
"""
from pandas.api.types import is_scalar as pandas_is_scalar
from .base import BasePandasDataset
return not isinstance(obj, BasePandasDataset) and pandas_is_scalar(obj)
def is_full_grab_slice(slc, sequence_len=None):
"""
Check that the passed slice grabs the whole sequence.
Parameters
----------
slc : slice
Slice object to check.
sequence_len : int, optional
Length of the sequence to index with the passed `slc`.
If not specified the function won't be able to check whether
``slc.stop`` is equal or greater than the sequence length to
consider `slc` to be a full-grab, and so, only slices with
``.stop is None`` are considered to be a full-grab.
Returns
-------
bool
"""
assert isinstance(slc, slice), "slice object required"
return (
slc.start in (None, 0)
and slc.step in (None, 1)
and (
slc.stop is None or (sequence_len is not None and slc.stop >= sequence_len)
)
)
def from_modin_frame_to_mi(df, sortorder=None, names=None):
"""
Make a pandas.MultiIndex from a DataFrame.
Parameters
----------
df : DataFrame
DataFrame to be converted to pandas.MultiIndex.
sortorder : int, default: None
Level of sortedness (must be lexicographically sorted by that
level).
names : list-like, optional
If no names are provided, use the column names, or tuple of column
names if the columns is a MultiIndex. If a sequence, overwrite
names with the given sequence.
Returns
-------
pandas.MultiIndex
The pandas.MultiIndex representation of the given DataFrame.
"""
from .dataframe import DataFrame
if isinstance(df, DataFrame):
from modin.error_message import ErrorMessage
ErrorMessage.default_to_pandas("`MultiIndex.from_frame`")
df = df._to_pandas()
return _original_pandas_MultiIndex_from_frame(df, sortorder, names)
def is_label(obj, label, axis=0):
"""
Check whether or not 'obj' contain column or index level with name 'label'.
Parameters
----------
obj : modin.pandas.DataFrame, modin.pandas.Series or modin.core.storage_formats.base.BaseQueryCompiler
Object to check.
label : object
Label name to check.
axis : {0, 1}, default: 0
Axis to search for `label` along.
Returns
-------
bool
True if check is successful, False otherwise.
"""
qc = getattr(obj, "_query_compiler", obj)
return hashable(label) and (
label in qc.get_axis(axis ^ 1) or label in qc.get_index_names(axis)
)
_original_pandas_MultiIndex_from_frame = MultiIndex.from_frame
MultiIndex.from_frame = from_modin_frame_to_mi