-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
pandas.py
188 lines (154 loc) · 4.11 KB
/
pandas.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
from typing import Optional
import numpy as np
from packaging.version import Version, parse
import pandas as pd
from pandas.util._decorators import (
Appender,
Substitution,
cache_readonly,
deprecate_kwarg,
)
__all__ = [
"assert_frame_equal",
"assert_index_equal",
"assert_series_equal",
"data_klasses",
"frequencies",
"is_numeric_dtype",
"testing",
"cache_readonly",
"deprecate_kwarg",
"Appender",
"Substitution",
"is_int_index",
"is_float_index",
"make_dataframe",
"to_numpy",
"PD_LT_1_0_0",
"get_cached_func",
"get_cached_doc",
"call_cached_func",
"PD_LT_1_4",
"PD_LT_2",
"MONTH_END",
"QUARTER_END",
"YEAR_END",
"FUTURE_STACK",
]
version = parse(pd.__version__)
PD_LT_2_2_0 = version < Version("2.1.99")
PD_LT_2_1_0 = version < Version("2.0.99")
PD_LT_1_0_0 = version < Version("0.99.0")
PD_LT_1_4 = version < Version("1.3.99")
PD_LT_2 = version < Version("1.9.99")
try:
from pandas.api.types import is_numeric_dtype
except ImportError:
from pandas.core.common import is_numeric_dtype
try:
from pandas.tseries import offsets as frequencies
except ImportError:
from pandas.tseries import frequencies
data_klasses = (pd.Series, pd.DataFrame)
try:
import pandas.testing as testing
except ImportError:
import pandas.util.testing as testing
assert_frame_equal = testing.assert_frame_equal
assert_index_equal = testing.assert_index_equal
assert_series_equal = testing.assert_series_equal
def is_int_index(index: pd.Index) -> bool:
"""
Check if an index is integral
Parameters
----------
index : pd.Index
Any numeric index
Returns
-------
bool
True if is an index with a standard integral type
"""
return (
isinstance(index, pd.Index)
and isinstance(index.dtype, np.dtype)
and np.issubdtype(index.dtype, np.integer)
)
def is_float_index(index: pd.Index) -> bool:
"""
Check if an index is floating
Parameters
----------
index : pd.Index
Any numeric index
Returns
-------
bool
True if an index with a standard numpy floating dtype
"""
return (
isinstance(index, pd.Index)
and isinstance(index.dtype, np.dtype)
and np.issubdtype(index.dtype, np.floating)
)
try:
from pandas._testing import makeDataFrame as make_dataframe
except ImportError:
import string
def rands_array(nchars, size, dtype="O"):
"""
Generate an array of byte strings.
"""
rands_chars = np.array(
list(string.ascii_letters + string.digits), dtype=(np.str_, 1)
)
retval = (
np.random.choice(rands_chars, size=nchars * np.prod(size))
.view((np.str_, nchars))
.reshape(size)
)
if dtype is None:
return retval
else:
return retval.astype(dtype)
def make_dataframe():
"""
Simple verion of pandas._testing.makeDataFrame
"""
n = 30
k = 4
index = pd.Index(rands_array(nchars=10, size=n), name=None)
data = {
c: pd.Series(np.random.randn(n), index=index)
for c in string.ascii_uppercase[:k]
}
return pd.DataFrame(data)
def to_numpy(po: pd.DataFrame) -> np.ndarray:
"""
Workaround legacy pandas lacking to_numpy
Parameters
----------
po : Pandas obkect
Returns
-------
ndarray
A numpy array
"""
try:
return po.to_numpy()
except AttributeError:
return po.values
def get_cached_func(cached_prop):
try:
return cached_prop.fget
except AttributeError:
return cached_prop.func
def call_cached_func(cached_prop, *args, **kwargs):
f = get_cached_func(cached_prop)
return f(*args, **kwargs)
def get_cached_doc(cached_prop) -> Optional[str]:
return get_cached_func(cached_prop).__doc__
MONTH_END = "M" if PD_LT_2_2_0 else "ME"
QUARTER_END = "Q" if PD_LT_2_2_0 else "QE"
YEAR_END = "Y" if PD_LT_2_2_0 else "YE"
FUTURE_STACK = {} if PD_LT_2_1_0 else {"future_stack": True}