|
5 | 5 |
|
6 | 6 | import narwhals as nw
|
7 | 7 |
|
8 |
| -df_raw = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) |
9 |
| -df = nw.DataFrame(df_raw, is_lazy=True) |
10 |
| -df_raw_2 = pd.DataFrame({"a": [1, 3], "c": [7, 9]}) |
11 |
| -df2 = nw.DataFrame(df_raw_2, is_lazy=True) |
| 8 | +# df_raw = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) |
| 9 | +# df = nw.DataFrame(df_raw, is_lazy=True) |
| 10 | +# df_raw_2 = pd.DataFrame({"a": [1, 3], "c": [7, 9]}) |
| 11 | +# df2 = nw.DataFrame(df_raw_2, is_lazy=True) |
12 | 12 |
|
13 |
| -result = df.sort("a", "b") |
14 |
| -print(nw.to_native(result)) |
| 13 | +# result = df.sort("a", "b") |
| 14 | +# print(nw.to_native(result)) |
15 | 15 |
|
16 |
| -result = df.filter(nw.col("a") > 1) |
17 |
| -print(nw.to_native(result)) |
| 16 | +# result = df.filter(nw.col("a") > 1) |
| 17 | +# print(nw.to_native(result)) |
18 | 18 |
|
19 |
| -result = df.with_columns( |
20 |
| - c=nw.col("a") + nw.col("b"), |
21 |
| - d=nw.col("a") - nw.col("a").mean(), |
22 |
| -) |
23 |
| -print(nw.to_native(result)) |
24 |
| -result = df.with_columns(nw.all() * 2) |
25 |
| -print(nw.to_native(result)) |
| 19 | +# result = df.with_columns( |
| 20 | +# c=nw.col("a") + nw.col("b"), |
| 21 | +# d=nw.col("a") - nw.col("a").mean(), |
| 22 | +# ) |
| 23 | +# print(nw.to_native(result)) |
| 24 | +# result = df.with_columns(nw.all() * 2) |
| 25 | +# print(nw.to_native(result)) |
26 | 26 |
|
27 |
| -result = df.with_columns(horizonal_sum=nw.sum_horizontal(nw.col("a"), nw.col("b"))) |
28 |
| -print(nw.to_native(result)) |
29 |
| -result = df.with_columns(horizonal_sum=nw.sum_horizontal("a", nw.col("b"))) |
30 |
| -print(nw.to_native(result)) |
| 27 | +# result = df.with_columns(horizonal_sum=nw.sum_horizontal(nw.col("a"), nw.col("b"))) |
| 28 | +# print(nw.to_native(result)) |
| 29 | +# result = df.with_columns(horizonal_sum=nw.sum_horizontal("a", nw.col("b"))) |
| 30 | +# print(nw.to_native(result)) |
31 | 31 |
|
32 | 32 |
|
33 |
| -result = df.select(nw.all().sum()) |
34 |
| -print(nw.to_native(result)) |
35 |
| -result = df.select(nw.col("a", "b") * 2) |
36 |
| -print(nw.to_native(result)) |
| 33 | +# result = df.select(nw.all().sum()) |
| 34 | +# print(nw.to_native(result)) |
| 35 | +# result = df.select(nw.col("a", "b") * 2) |
| 36 | +# print(nw.to_native(result)) |
37 | 37 |
|
38 |
| -# # TODO! |
39 |
| -# # result = ( |
40 |
| -# # df.collect() |
41 |
| -# # .group_by("b") |
42 |
| -# # .agg( |
43 |
| -# # nw.all().sum(), |
44 |
| -# # ) |
45 |
| -# # ) |
46 |
| -# # print(nw.to_native(result)) |
47 |
| - |
48 |
| -result = ( |
49 |
| - df.collect() |
50 |
| - .group_by("b") |
51 |
| - .agg( |
52 |
| - nw.col("a").sum(), |
53 |
| - simple=nw.col("a").sum(), |
54 |
| - complex=(nw.col("a") + 1).sum(), |
55 |
| - other=nw.sum("a"), |
56 |
| - ) |
57 |
| -) |
58 |
| -print(nw.to_native(result)) |
59 |
| -print("multiple simple") |
60 |
| -result = ( |
61 |
| - df.collect() |
62 |
| - .group_by("b") |
63 |
| - .agg( |
64 |
| - nw.col("a", "z").sum(), |
65 |
| - ) |
66 |
| -) |
67 |
| -print(nw.to_native(result)) |
| 38 | +# # # TODO! |
| 39 | +# # # result = ( |
| 40 | +# # # df.collect() |
| 41 | +# # # .group_by("b") |
| 42 | +# # # .agg( |
| 43 | +# # # nw.all().sum(), |
| 44 | +# # # ) |
| 45 | +# # # ) |
| 46 | +# # # print(nw.to_native(result)) |
| 47 | + |
| 48 | +# result = ( |
| 49 | +# df.collect() |
| 50 | +# .group_by("b") |
| 51 | +# .agg( |
| 52 | +# nw.col("a").sum(), |
| 53 | +# simple=nw.col("a").sum(), |
| 54 | +# complex=(nw.col("a") + 1).sum(), |
| 55 | +# other=nw.sum("a"), |
| 56 | +# ) |
| 57 | +# ) |
| 58 | +# print(nw.to_native(result)) |
| 59 | +# print("multiple simple") |
| 60 | +# result = ( |
| 61 | +# df.collect() |
| 62 | +# .group_by("b") |
| 63 | +# .agg( |
| 64 | +# nw.col("a", "z").sum(), |
| 65 | +# ) |
| 66 | +# ) |
| 67 | +# print(nw.to_native(result)) |
68 | 68 |
|
69 |
| -result = df.join(df2, left_on="a", right_on="a") |
70 |
| -print(nw.to_native(result)) |
| 69 | +# result = df.join(df2, left_on="a", right_on="a") |
| 70 | +# print(nw.to_native(result)) |
71 | 71 |
|
72 | 72 |
|
73 |
| -result = df.rename({"a": "a_new", "b": "b_new"}) |
74 |
| -print(nw.to_native(result)) |
| 73 | +# result = df.rename({"a": "a_new", "b": "b_new"}) |
| 74 | +# print(nw.to_native(result)) |
75 | 75 |
|
76 |
| -result = df.collect().to_dict() |
77 |
| -print(result) |
78 |
| -print(polars.from_pandas(nw.to_native(df)).to_dict()) |
| 76 | +# result = df.collect().to_dict() |
| 77 | +# print(result) |
| 78 | +# print(polars.from_pandas(nw.to_native(df)).to_dict()) |
79 | 79 |
|
80 |
| -result = df.collect().to_dict(as_series=False) |
81 |
| -print("this") |
82 |
| -print(result) |
83 |
| -print("that") |
84 |
| -print(polars.from_pandas(nw.to_native(df)).to_dict(as_series=False)) |
| 80 | +# result = df.collect().to_dict(as_series=False) |
| 81 | +# print("this") |
| 82 | +# print(result) |
| 83 | +# print("that") |
| 84 | +# print(polars.from_pandas(nw.to_native(df)).to_dict(as_series=False)) |
85 | 85 |
|
86 |
| -agg = (nw.col("b") - nw.col("z").mean()).mean() |
87 |
| -print(nw.to_native(df.with_columns(d=agg))) |
88 |
| -result = df.group_by("a").agg(agg) |
89 |
| -print(nw.to_native(result)) |
| 86 | +# agg = (nw.col("b") - nw.col("z").mean()).mean() |
| 87 | +# print(nw.to_native(df.with_columns(d=agg))) |
| 88 | +# result = df.group_by("a").agg(agg) |
| 89 | +# print(nw.to_native(result)) |
90 | 90 |
|
91 |
| -print(nw.col("a") + nw.col("b")) |
92 |
| -print(nw.col("a", "b").sum()) |
| 91 | +# print(nw.col("a") + nw.col("b")) |
| 92 | +# print(nw.col("a", "b").sum()) |
93 | 93 |
|
94 |
| -result = df.select(nw.col("a", "b").sum()) |
95 |
| -print(nw.to_native(result)) |
| 94 | +# result = df.select(nw.col("a", "b").sum()) |
| 95 | +# print(nw.to_native(result)) |
96 | 96 |
|
97 |
| -print(df.schema) |
| 97 | +# print(df.schema) |
98 | 98 | # print(df.schema['a'].is_numeric())
|
99 | 99 |
|
100 |
| -# df_raw = pd.DataFrame({ |
101 |
| -# "a": [1, 3, 2], |
102 |
| -# "b": [4., 4, 6], |
103 |
| -# 'c': ['a', 'b', 'c'], |
104 |
| -# 'd': [True, False, True], |
105 |
| -# }) |
106 |
| -# df, pl = narwhals.to_polars_api(df_raw) |
| 100 | +df_raw = pd.DataFrame( |
| 101 | + { |
| 102 | + "a": [1, 3, 2], |
| 103 | + "b": [4.0, 4, 6], |
| 104 | + "c": ["a", "b", "c"], |
| 105 | + "d": [True, False, True], |
| 106 | + } |
| 107 | +) |
| 108 | +df = nw.DataFrame(df_raw) |
107 | 109 | # print(df.schema)
|
108 | 110 | # print(df.schema['a'].is_numeric())
|
109 | 111 | # print(df.schema['b'].is_numeric())
|
110 | 112 | # print(df.schema['c'].is_numeric())
|
111 | 113 | # print(df.schema['d'].is_numeric())
|
112 | 114 |
|
113 |
| -# result = df.with_columns(nw.col('a').cast(pl.Float32)) |
| 115 | +# result = df.with_columns(nw.col('a').cast(nw.Float32)) |
114 | 116 | # print(nw.to_native(result))
|
115 |
| -# print(result._dataframe.dtypes) |
| 117 | +# print(result._dataframe._dataframe.dtypes) |
116 | 118 |
|
117 | 119 | # print(df.schema)
|
118 |
| -# result = df.select([col for (col, dtype) in df.schema.items() if dtype == pl.Float64]) |
119 |
| -# print(nw.to_native(result)) |
120 |
| -# print(result._dataframe.dtypes) |
121 |
| - |
122 |
| -# print(nw.all() + nw.col("a")) |
123 |
| -# result = df.select(nw.all() + nw.col("a")) |
| 120 | +# result = df.select([col for (col, dtype) in df.schema.items() if dtype == nw.Float64]) |
124 | 121 | # print(nw.to_native(result))
|
125 |
| -# print(result._dataframe.dtypes) |
| 122 | +# print(result._dataframe._dataframe.dtypes) |
126 | 123 |
|
127 |
| -# print(result.collect()) |
| 124 | +result = df.select("a", "b").select(nw.all() + nw.col("a")) |
| 125 | +print(nw.to_native(result)) |
0 commit comments