-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathtest_api_typing.py
218 lines (145 loc) · 4.97 KB
/
test_api_typing.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""Test module for classes in pandas.api.typing."""
import numpy as np
import pandas as pd
from pandas._testing import ensure_clean
from pandas.api.typing import (
DataFrameGroupBy,
DatetimeIndexResamplerGroupby,
Expanding,
ExpandingGroupby,
ExponentialMovingWindow,
ExponentialMovingWindowGroupby,
JsonReader,
NaTType,
NAType,
PeriodIndexResamplerGroupby,
Resampler,
Rolling,
RollingGroupby,
SeriesGroupBy,
StataReader,
TimedeltaIndexResamplerGroupby,
TimeGrouper,
Window,
)
import pytest
from typing_extensions import (
TypeAlias,
assert_type,
)
from tests import check
from pandas.io.json._json import read_json
ResamplerGroupBy: TypeAlias = (
DatetimeIndexResamplerGroupby
| PeriodIndexResamplerGroupby
| TimedeltaIndexResamplerGroupby
)
def test_dataframegroupby():
df = pd.DataFrame({"a": [1, 2, 3]})
group = df.groupby("a")
def f1(gb: DataFrameGroupBy):
check(gb, DataFrameGroupBy)
f1(group)
def test_seriesgroupby():
sr = pd.Series([1, 2, 3], index=pd.Index(["a", "b", "a"]))
def f1(gb: SeriesGroupBy):
check(gb, SeriesGroupBy)
f1(sr.groupby(level=0))
def tests_datetimeindexersamplergroupby() -> None:
idx = pd.date_range("1999-1-1", periods=365, freq="D")
df = pd.DataFrame(
np.random.standard_normal((365, 2)), index=idx, columns=["col1", "col2"]
)
gb_df = df.groupby("col2")
def f1(gb: ResamplerGroupBy):
check(gb, DatetimeIndexResamplerGroupby)
f1(gb_df.resample("ME"))
def test_timedeltaindexresamplergroupby() -> None:
idx = pd.TimedeltaIndex(["0 days", "1 days", "2 days", "3 days", "4 days"])
df = pd.DataFrame(
np.random.standard_normal((5, 2)), index=idx, columns=["col1", "col2"]
)
gb_df = df.groupby("col2")
def f1(gb: ResamplerGroupBy):
check(gb, TimedeltaIndexResamplerGroupby)
f1(gb_df.resample("1D"))
@pytest.mark.skip("Resampling with a PeriodIndex is deprecated.")
def test_periodindexresamplergroupby() -> None:
idx = pd.period_range("2020-01-28 09:00", periods=4, freq="D")
df = pd.DataFrame(data=4 * [range(2)], index=idx, columns=["a", "b"])
def f1(gb: ResamplerGroupBy):
check(gb, PeriodIndexResamplerGroupby)
f1(df.groupby("a").resample("3min"))
def test_natype() -> None:
i64dt = pd.Int64Dtype()
check(assert_type(i64dt.na_value, NAType), NAType)
def test_nattype() -> None:
td = pd.Timedelta("1 day")
as_nat = pd.NaT
check(assert_type(td + as_nat, NaTType), NaTType)
def test_expanding() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: Expanding):
check(gb, Expanding)
f1(df.expanding())
def test_expanding_groubpy() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: ExpandingGroupby):
check(gb, ExpandingGroupby)
f1(df.groupby("B").expanding())
def test_ewm() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: ExponentialMovingWindow):
check(gb, ExponentialMovingWindow)
f1(df.ewm(2))
def test_ewm_groubpy() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: ExponentialMovingWindowGroupby):
check(gb, ExponentialMovingWindowGroupby)
f1(df.groupby("B").ewm(2))
def test_json_reader() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: JsonReader):
check(gb, JsonReader)
with ensure_clean() as path:
check(assert_type(df.to_json(path), None), type(None))
json_reader = read_json(path, chunksize=1, lines=True)
f1(json_reader)
json_reader.close()
def test_resampler() -> None:
s = pd.Series([1, 2, 3, 4, 5], index=pd.date_range("20130101", periods=5, freq="s"))
def f1(gb: Resampler):
check(gb, Resampler)
f1(s.resample("3min"))
def test_rolling() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: Rolling):
check(gb, Rolling)
f1(df.rolling(2))
def test_rolling_groupby() -> None:
df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
def f1(gb: RollingGroupby):
check(gb, RollingGroupby)
f1(df.groupby("B").rolling(2))
def test_timegrouper() -> None:
grouper = pd.Grouper(key="Publish date", freq="1W")
def f1(gb: TimeGrouper):
check(gb, TimeGrouper)
f1(grouper)
def test_window() -> None:
ser = pd.Series([0, 1, 5, 2, 8])
def f1(gb: Window):
check(gb, Window)
f1(ser.rolling(2, win_type="gaussian"))
def test_statereader() -> None:
df = pd.DataFrame([[1, 2], [3, 4]], columns=["col_1", "col_2"])
time_stamp = pd.Timestamp(2000, 2, 29, 14, 21)
variable_labels = {"col_1": "This is an example"}
with ensure_clean() as path:
df.to_stata(
path, time_stamp=time_stamp, variable_labels=variable_labels, version=None
)
def f1(gb: StataReader):
check(gb, StataReader)
with StataReader(path) as reader:
f1(reader)