forked from narwhals-dev/narwhals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.py
131 lines (107 loc) · 3.09 KB
/
t.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
# ruff: noqa
# type: ignore
import polars
import pandas as pd
import polars as pl
import narwhals as nw
df_raw = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
df = nw.DataFrame(df_raw, features=["lazy"])
df_raw_2 = pd.DataFrame({"a": [1, 3], "c": [7, 9]})
df2 = nw.DataFrame(df_raw_2, features=["lazy"])
result = df.sort("a", "b")
print(nw.to_native(result))
result = df.filter(nw.col("a") > 1)
print(nw.to_native(result))
result = df.with_columns(
c=nw.col("a") + nw.col("b"),
d=nw.col("a") - nw.col("a").mean(),
)
print(nw.to_native(result))
result = df.with_columns(nw.all() * 2)
print(nw.to_native(result))
result = df.with_columns(horizonal_sum=nw.sum_horizontal(nw.col("a"), nw.col("b")))
print(nw.to_native(result))
result = df.with_columns(horizonal_sum=nw.sum_horizontal("a", nw.col("b")))
print(nw.to_native(result))
result = df.select(nw.all().sum())
print(nw.to_native(result))
result = df.select(nw.col("a", "b") * 2)
print(nw.to_native(result))
# # TODO!
# # result = (
# # df.collect()
# # .group_by("b")
# # .agg(
# # nw.all().sum(),
# # )
# # )
# # print(nw.to_native(result))
result = (
df.collect()
.group_by("b")
.agg(
nw.col("a").sum(),
simple=nw.col("a").sum(),
complex=(nw.col("a") + 1).sum(),
other=nw.sum("a"),
)
)
print(nw.to_native(result))
print("multiple simple")
result = (
df.collect()
.group_by("b")
.agg(
nw.col("a", "z").sum(),
)
)
print(nw.to_native(result))
result = df.join(df2, left_on="a", right_on="a")
print(nw.to_native(result))
result = df.rename({"a": "a_new", "b": "b_new"})
print(nw.to_native(result))
result = df.collect().to_dict()
print(result)
print(polars.from_pandas(nw.to_native(df)).to_dict())
result = df.collect().to_dict(as_series=False)
print("this")
print(result)
print("that")
print(polars.from_pandas(nw.to_native(df)).to_dict(as_series=False))
agg = (nw.col("b") - nw.col("z").mean()).mean()
print(nw.to_native(df.with_columns(d=agg)))
result = df.group_by("a").agg(agg)
print(nw.to_native(result))
print(nw.col("a") + nw.col("b"))
print(nw.col("a", "b").sum())
result = df.select(nw.col("a", "b").sum())
print(nw.to_native(result))
print(df.schema)
print(df.schema["a"].is_numeric())
df_raw = pd.DataFrame(
{
"a": [1, 3, 2],
"b": [4.0, 4, 6],
"c": ["a", "b", "c"],
"d": [True, False, True],
}
)
df = nw.DataFrame(df_raw)
print(df.schema)
print(df.schema["a"].is_numeric())
print(df.schema["b"].is_numeric())
print(df.schema["c"].is_numeric())
print(df.schema["d"].is_numeric())
result = df.with_columns(nw.col("a").cast(nw.Float32))
print(nw.to_native(result))
print(result._dataframe._dataframe.dtypes)
print(df.schema)
result = df.select([col for (col, dtype) in df.schema.items() if dtype == nw.Float64])
print(nw.to_native(result))
print(result._dataframe._dataframe.dtypes)
result = df.select("a", "b").select(nw.all() + nw.col("a"))
print(nw.to_native(result))
df = nw.DataFrame(df_raw, features=["eager"])
print(df["a"].mean())
df = nw.DataFrame(pl.from_pandas(df_raw), features=["eager"])
print(df["a"].mean())