PythonSTB commited on
Commit
3f4ac3f
·
verified ·
1 Parent(s): 62a0fd0

Upload pandas/Test_Pandas.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pandas/Test_Pandas.py +282 -0
pandas/Test_Pandas.py ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Generated by RIMI
3
+ """
4
+ import sys
5
+ import traceback
6
+
7
+ passed = 0
8
+ failed = 0
9
+ skipped = 0
10
+
11
+ def test(name, func):
12
+ global passed, failed, skipped
13
+ try:
14
+ result = func()
15
+ if isinstance(result, str) and result == "SKIP":
16
+ skipped += 1
17
+ print(f" SKIP #{passed+failed+skipped:02d} {name}")
18
+ else:
19
+ passed += 1
20
+ print(f" OK #{passed+failed+skipped:02d} {name}")
21
+ except Exception as e:
22
+ failed += 1
23
+ print(f" FAIL #{passed+failed+skipped:02d} {name}: {e}")
24
+ traceback.print_exc()
25
+
26
+ print("=" * 60)
27
+ print("pandas 2.3.3 — Android norelro test")
28
+ print("Python", sys.version)
29
+ print("=" * 60)
30
+
31
+ # 1. import pandas
32
+ test("import pandas", lambda: __import__("pandas"))
33
+
34
+ # 2. version check
35
+ test("pandas.__version__", lambda: None if __import__("pandas").__version__ == "2.3.3" else (_ for _ in ()).throw(Exception(f"wrong version")))
36
+
37
+ # 3. import numpy (bundled dep)
38
+ test("import numpy (bundled dep)", lambda: __import__("numpy"))
39
+
40
+ # 4. DataFrame basics
41
+ test("DataFrame create", lambda: __import__("pandas").DataFrame({"a": [1, 2], "b": [3, 4]}))
42
+
43
+ # 5. DataFrame shape
44
+ def test_shape():
45
+ import pandas as pd
46
+ df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
47
+ assert df.shape == (3, 2), f"wrong shape: {df.shape}"
48
+ test("DataFrame shape", test_shape)
49
+
50
+ # 6. DataFrame head/tail
51
+ def test_head_tail():
52
+ import pandas as pd
53
+ df = pd.DataFrame({"x": range(100)})
54
+ assert len(df.head(5)) == 5
55
+ assert len(df.tail(5)) == 5
56
+ test("DataFrame head/tail", test_head_tail)
57
+
58
+ # 7. DataFrame dtypes
59
+ def test_dtypes():
60
+ import pandas as pd
61
+ df = pd.DataFrame({"a": [1, 2], "b": [1.0, 2.0], "c": ["x", "y"]})
62
+ assert df.dtypes["a"].name == "int64"
63
+ assert df.dtypes["b"].name == "float64"
64
+ assert df.dtypes["c"].name == "object"
65
+ test("DataFrame dtypes", test_dtypes)
66
+
67
+ # 8. DataFrame describe
68
+ def test_describe():
69
+ import pandas as pd
70
+ df = pd.DataFrame({"a": [1, 2, 3, 4, 5]})
71
+ desc = df.describe()
72
+ assert desc.loc["mean", "a"] == 3.0
73
+ test("DataFrame describe", test_describe)
74
+
75
+ # 9. DataFrame groupby
76
+ def test_groupby():
77
+ import pandas as pd
78
+ df = pd.DataFrame({"g": ["a", "a", "b"], "v": [1, 2, 3]})
79
+ result = df.groupby("g")["v"].sum()
80
+ assert result["a"] == 3
81
+ assert result["b"] == 3
82
+ test("DataFrame groupby", test_groupby)
83
+
84
+ # 10. DataFrame sort
85
+ def test_sort():
86
+ import pandas as pd
87
+ df = pd.DataFrame({"a": [3, 1, 2]})
88
+ df_sorted = df.sort_values("a")
89
+ assert list(df_sorted["a"]) == [1, 2, 3]
90
+ test("DataFrame sort_values", test_sort)
91
+
92
+ # 11. DataFrame apply
93
+ def test_apply():
94
+ import pandas as pd
95
+ df = pd.DataFrame({"a": [1, 2, 3]})
96
+ result = df["a"].apply(lambda x: x * 2)
97
+ assert list(result) == [2, 4, 6]
98
+ test("DataFrame apply", test_apply)
99
+
100
+ # 12. DataFrame merge
101
+ def test_merge():
102
+ import pandas as pd
103
+ a = pd.DataFrame({"k": [1, 2], "v": ["a", "b"]})
104
+ b = pd.DataFrame({"k": [1, 2], "w": ["c", "d"]})
105
+ m = a.merge(b, on="k")
106
+ assert list(m.columns) == ["k", "v", "w"]
107
+ assert len(m) == 2
108
+ test("DataFrame merge", test_merge)
109
+
110
+ # 13. DataFrame concat
111
+ def test_concat():
112
+ import pandas as pd
113
+ a = pd.DataFrame({"a": [1, 2]})
114
+ b = pd.DataFrame({"a": [3, 4]})
115
+ c = pd.concat([a, b], ignore_index=True)
116
+ assert list(c["a"]) == [1, 2, 3, 4]
117
+ test("DataFrame concat", test_concat)
118
+
119
+ # 14. DataFrame fillna/dropna
120
+ def test_fillna():
121
+ import pandas as pd
122
+ df = pd.DataFrame({"a": [1, None, 3]})
123
+ filled = df.fillna(0)
124
+ assert list(filled["a"]) == [1.0, 0.0, 3.0]
125
+ dropped = df.dropna()
126
+ assert len(dropped) == 2
127
+ test("DataFrame fillna/dropna", test_fillna)
128
+
129
+ # 15. DataFrame pivot
130
+ def test_pivot():
131
+ import pandas as pd
132
+ df = pd.DataFrame({"r": ["a", "a"], "c": ["x", "y"], "v": [1, 2]})
133
+ p = df.pivot(index="r", columns="c", values="v")
134
+ assert p.loc["a", "x"] == 1
135
+ assert p.loc["a", "y"] == 2
136
+ test("DataFrame pivot", test_pivot)
137
+
138
+ # 16. DataFrame melt
139
+ def test_melt():
140
+ import pandas as pd
141
+ df = pd.DataFrame({"id": [1], "x": [2], "y": [3]})
142
+ m = pd.melt(df, id_vars=["id"])
143
+ assert len(m) == 2
144
+ test("DataFrame melt", test_melt)
145
+
146
+ # 17. Series operations
147
+ def test_series():
148
+ import pandas as pd
149
+ s = pd.Series([1, 2, 3, 4])
150
+ assert s.sum() == 10
151
+ assert s.mean() == 2.5
152
+ assert s.max() == 4
153
+ assert s.min() == 1
154
+ test("Series agg ops", test_series)
155
+
156
+ # 18. DatetimeIndex
157
+ def test_datetime():
158
+ import pandas as pd
159
+ dates = pd.date_range("2024-01-01", periods=5, freq="D")
160
+ assert len(dates) == 5
161
+ assert dates[0].year == 2024
162
+ assert dates[0].month == 1
163
+ assert dates[0].day == 1
164
+ test("DatetimeIndex", test_datetime)
165
+
166
+ # 19. Timedelta
167
+ def test_timedelta():
168
+ import pandas as pd
169
+ td = pd.Timedelta("1 day 2 hours")
170
+ assert td.total_seconds() == 93600.0
171
+ test("Timedelta", test_timedelta)
172
+
173
+ # 20. read_csv / to_csv roundtrip
174
+ def test_csv():
175
+ import pandas as pd, os, tempfile
176
+ df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
177
+ with tempfile.NamedTemporaryFile(suffix=".csv", delete=False, mode="w") as f:
178
+ df.to_csv(f, index=False)
179
+ tmp = f.name
180
+ df2 = pd.read_csv(tmp)
181
+ os.unlink(tmp)
182
+ assert list(df2["a"]) == [1, 2, 3]
183
+ assert list(df2["b"]) == ["x", "y", "z"]
184
+ test("read_csv / to_csv roundtrip", test_csv)
185
+
186
+ # 21. read_json / to_json roundtrip
187
+ def test_json():
188
+ import pandas as pd, os, tempfile
189
+ df = pd.DataFrame({"a": [1, 2], "b": [3.0, 4.0]})
190
+ with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
191
+ df.to_json(f, orient="records")
192
+ tmp = f.name
193
+ df2 = pd.read_json(tmp, orient="records")
194
+ os.unlink(tmp)
195
+ assert list(df2["a"]) == [1, 2]
196
+ test("read_json / to_json roundtrip", test_json)
197
+
198
+ # 22. DataFrame value_counts
199
+ def test_value_counts():
200
+ import pandas as pd
201
+ s = pd.Series(["a", "b", "a", "a", "b"])
202
+ vc = s.value_counts()
203
+ assert vc["a"] == 3
204
+ assert vc["b"] == 2
205
+ test("Series value_counts", test_value_counts)
206
+
207
+ # 23. DataFrame corr
208
+ def test_corr():
209
+ import pandas as pd
210
+ df = pd.DataFrame({"a": [1, 2, 3], "b": [2, 4, 6]})
211
+ corr = df["a"].corr(df["b"])
212
+ assert abs(corr - 1.0) < 1e-10
213
+ test("DataFrame corr", test_corr)
214
+
215
+ # 24. DataFrame map/replace
216
+ def test_replace():
217
+ import pandas as pd
218
+ s = pd.Series([1, 2, 3])
219
+ r = s.replace({1: "a", 2: "b", 3: "c"})
220
+ assert list(r) == ["a", "b", "c"]
221
+ test("Series replace", test_replace)
222
+
223
+ # 25. MultiIndex
224
+ def test_multiindex():
225
+ import pandas as pd
226
+ arrays = [["a", "a", "b", "b"], [1, 2, 1, 2]]
227
+ idx = pd.MultiIndex.from_arrays(arrays, names=["l1", "l2"])
228
+ df = pd.DataFrame({"v": [10, 20, 30, 40]}, index=idx)
229
+ assert df.loc["a", 1].iloc[0] == 10
230
+ test("MultiIndex", test_multiindex)
231
+
232
+ # 26. DataFrame to_numpy
233
+ def test_to_numpy():
234
+ import pandas as pd
235
+ df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
236
+ arr = df.to_numpy()
237
+ assert arr.shape == (2, 2)
238
+ assert arr[0, 0] == 1
239
+ assert arr[1, 1] == 4
240
+ test("DataFrame to_numpy", test_to_numpy)
241
+
242
+ # 27. Categorical
243
+ def test_categorical():
244
+ import pandas as pd
245
+ s = pd.Categorical(["a", "b", "a", "c"])
246
+ assert len(s) == 4
247
+ assert s.categories.tolist() == ["a", "b", "c"]
248
+ test("Categorical", test_categorical)
249
+
250
+ # 28. DataFrame assign
251
+ def test_assign():
252
+ import pandas as pd
253
+ df = pd.DataFrame({"a": [1, 2]})
254
+ df2 = df.assign(b=df["a"] * 10)
255
+ assert list(df2["b"]) == [10, 20]
256
+ test("DataFrame assign", test_assign)
257
+
258
+ # 29. DataFrame pipe
259
+ def test_pipe():
260
+ import pandas as pd
261
+ df = pd.DataFrame({"a": [1, 2, 3]})
262
+ def add_one(data):
263
+ return data.assign(b=data["a"] + 1)
264
+ df2 = df.pipe(add_one)
265
+ assert list(df2["b"]) == [2, 3, 4]
266
+ test("DataFrame pipe", test_pipe)
267
+
268
+ # 30. DataFrame nunique/nlargest
269
+ def test_nlargest():
270
+ import pandas as pd
271
+ df = pd.DataFrame({"a": [10, 1, 5, 20, 3]})
272
+ top = df.nlargest(2, "a")
273
+ assert list(top["a"]) == [20, 10]
274
+ test("DataFrame nlargest", test_nlargest)
275
+
276
+ print()
277
+ print("=" * 60)
278
+ print(f"RESULT: {passed} PASS, {failed} FAIL, {skipped} SKIP")
279
+ print("=" * 60)
280
+
281
+ if failed > 0:
282
+ sys.exit(1)