File size: 8,392 Bytes
3f4ac3f | 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | """
Generated by RIMI
"""
import sys
import traceback
passed = 0
failed = 0
skipped = 0
def test(name, func):
global passed, failed, skipped
try:
result = func()
if isinstance(result, str) and result == "SKIP":
skipped += 1
print(f" SKIP #{passed+failed+skipped:02d} {name}")
else:
passed += 1
print(f" OK #{passed+failed+skipped:02d} {name}")
except Exception as e:
failed += 1
print(f" FAIL #{passed+failed+skipped:02d} {name}: {e}")
traceback.print_exc()
print("=" * 60)
print("pandas 2.3.3 — Android norelro test")
print("Python", sys.version)
print("=" * 60)
# 1. import pandas
test("import pandas", lambda: __import__("pandas"))
# 2. version check
test("pandas.__version__", lambda: None if __import__("pandas").__version__ == "2.3.3" else (_ for _ in ()).throw(Exception(f"wrong version")))
# 3. import numpy (bundled dep)
test("import numpy (bundled dep)", lambda: __import__("numpy"))
# 4. DataFrame basics
test("DataFrame create", lambda: __import__("pandas").DataFrame({"a": [1, 2], "b": [3, 4]}))
# 5. DataFrame shape
def test_shape():
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
assert df.shape == (3, 2), f"wrong shape: {df.shape}"
test("DataFrame shape", test_shape)
# 6. DataFrame head/tail
def test_head_tail():
import pandas as pd
df = pd.DataFrame({"x": range(100)})
assert len(df.head(5)) == 5
assert len(df.tail(5)) == 5
test("DataFrame head/tail", test_head_tail)
# 7. DataFrame dtypes
def test_dtypes():
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [1.0, 2.0], "c": ["x", "y"]})
assert df.dtypes["a"].name == "int64"
assert df.dtypes["b"].name == "float64"
assert df.dtypes["c"].name == "object"
test("DataFrame dtypes", test_dtypes)
# 8. DataFrame describe
def test_describe():
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3, 4, 5]})
desc = df.describe()
assert desc.loc["mean", "a"] == 3.0
test("DataFrame describe", test_describe)
# 9. DataFrame groupby
def test_groupby():
import pandas as pd
df = pd.DataFrame({"g": ["a", "a", "b"], "v": [1, 2, 3]})
result = df.groupby("g")["v"].sum()
assert result["a"] == 3
assert result["b"] == 3
test("DataFrame groupby", test_groupby)
# 10. DataFrame sort
def test_sort():
import pandas as pd
df = pd.DataFrame({"a": [3, 1, 2]})
df_sorted = df.sort_values("a")
assert list(df_sorted["a"]) == [1, 2, 3]
test("DataFrame sort_values", test_sort)
# 11. DataFrame apply
def test_apply():
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]})
result = df["a"].apply(lambda x: x * 2)
assert list(result) == [2, 4, 6]
test("DataFrame apply", test_apply)
# 12. DataFrame merge
def test_merge():
import pandas as pd
a = pd.DataFrame({"k": [1, 2], "v": ["a", "b"]})
b = pd.DataFrame({"k": [1, 2], "w": ["c", "d"]})
m = a.merge(b, on="k")
assert list(m.columns) == ["k", "v", "w"]
assert len(m) == 2
test("DataFrame merge", test_merge)
# 13. DataFrame concat
def test_concat():
import pandas as pd
a = pd.DataFrame({"a": [1, 2]})
b = pd.DataFrame({"a": [3, 4]})
c = pd.concat([a, b], ignore_index=True)
assert list(c["a"]) == [1, 2, 3, 4]
test("DataFrame concat", test_concat)
# 14. DataFrame fillna/dropna
def test_fillna():
import pandas as pd
df = pd.DataFrame({"a": [1, None, 3]})
filled = df.fillna(0)
assert list(filled["a"]) == [1.0, 0.0, 3.0]
dropped = df.dropna()
assert len(dropped) == 2
test("DataFrame fillna/dropna", test_fillna)
# 15. DataFrame pivot
def test_pivot():
import pandas as pd
df = pd.DataFrame({"r": ["a", "a"], "c": ["x", "y"], "v": [1, 2]})
p = df.pivot(index="r", columns="c", values="v")
assert p.loc["a", "x"] == 1
assert p.loc["a", "y"] == 2
test("DataFrame pivot", test_pivot)
# 16. DataFrame melt
def test_melt():
import pandas as pd
df = pd.DataFrame({"id": [1], "x": [2], "y": [3]})
m = pd.melt(df, id_vars=["id"])
assert len(m) == 2
test("DataFrame melt", test_melt)
# 17. Series operations
def test_series():
import pandas as pd
s = pd.Series([1, 2, 3, 4])
assert s.sum() == 10
assert s.mean() == 2.5
assert s.max() == 4
assert s.min() == 1
test("Series agg ops", test_series)
# 18. DatetimeIndex
def test_datetime():
import pandas as pd
dates = pd.date_range("2024-01-01", periods=5, freq="D")
assert len(dates) == 5
assert dates[0].year == 2024
assert dates[0].month == 1
assert dates[0].day == 1
test("DatetimeIndex", test_datetime)
# 19. Timedelta
def test_timedelta():
import pandas as pd
td = pd.Timedelta("1 day 2 hours")
assert td.total_seconds() == 93600.0
test("Timedelta", test_timedelta)
# 20. read_csv / to_csv roundtrip
def test_csv():
import pandas as pd, os, tempfile
df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
with tempfile.NamedTemporaryFile(suffix=".csv", delete=False, mode="w") as f:
df.to_csv(f, index=False)
tmp = f.name
df2 = pd.read_csv(tmp)
os.unlink(tmp)
assert list(df2["a"]) == [1, 2, 3]
assert list(df2["b"]) == ["x", "y", "z"]
test("read_csv / to_csv roundtrip", test_csv)
# 21. read_json / to_json roundtrip
def test_json():
import pandas as pd, os, tempfile
df = pd.DataFrame({"a": [1, 2], "b": [3.0, 4.0]})
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
df.to_json(f, orient="records")
tmp = f.name
df2 = pd.read_json(tmp, orient="records")
os.unlink(tmp)
assert list(df2["a"]) == [1, 2]
test("read_json / to_json roundtrip", test_json)
# 22. DataFrame value_counts
def test_value_counts():
import pandas as pd
s = pd.Series(["a", "b", "a", "a", "b"])
vc = s.value_counts()
assert vc["a"] == 3
assert vc["b"] == 2
test("Series value_counts", test_value_counts)
# 23. DataFrame corr
def test_corr():
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [2, 4, 6]})
corr = df["a"].corr(df["b"])
assert abs(corr - 1.0) < 1e-10
test("DataFrame corr", test_corr)
# 24. DataFrame map/replace
def test_replace():
import pandas as pd
s = pd.Series([1, 2, 3])
r = s.replace({1: "a", 2: "b", 3: "c"})
assert list(r) == ["a", "b", "c"]
test("Series replace", test_replace)
# 25. MultiIndex
def test_multiindex():
import pandas as pd
arrays = [["a", "a", "b", "b"], [1, 2, 1, 2]]
idx = pd.MultiIndex.from_arrays(arrays, names=["l1", "l2"])
df = pd.DataFrame({"v": [10, 20, 30, 40]}, index=idx)
assert df.loc["a", 1].iloc[0] == 10
test("MultiIndex", test_multiindex)
# 26. DataFrame to_numpy
def test_to_numpy():
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
arr = df.to_numpy()
assert arr.shape == (2, 2)
assert arr[0, 0] == 1
assert arr[1, 1] == 4
test("DataFrame to_numpy", test_to_numpy)
# 27. Categorical
def test_categorical():
import pandas as pd
s = pd.Categorical(["a", "b", "a", "c"])
assert len(s) == 4
assert s.categories.tolist() == ["a", "b", "c"]
test("Categorical", test_categorical)
# 28. DataFrame assign
def test_assign():
import pandas as pd
df = pd.DataFrame({"a": [1, 2]})
df2 = df.assign(b=df["a"] * 10)
assert list(df2["b"]) == [10, 20]
test("DataFrame assign", test_assign)
# 29. DataFrame pipe
def test_pipe():
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]})
def add_one(data):
return data.assign(b=data["a"] + 1)
df2 = df.pipe(add_one)
assert list(df2["b"]) == [2, 3, 4]
test("DataFrame pipe", test_pipe)
# 30. DataFrame nunique/nlargest
def test_nlargest():
import pandas as pd
df = pd.DataFrame({"a": [10, 1, 5, 20, 3]})
top = df.nlargest(2, "a")
assert list(top["a"]) == [20, 10]
test("DataFrame nlargest", test_nlargest)
print()
print("=" * 60)
print(f"RESULT: {passed} PASS, {failed} FAIL, {skipped} SKIP")
print("=" * 60)
if failed > 0:
sys.exit(1)
|