我手头有一个相当大的数据框。将其自身加入需要一些时间。但我想将它们加入一些条件,这可能会使生成的数据帧小得多。我的问题是如何利用这些条件使条件连接比普通完全连接更快?
下面的代码用于说明:
import time
import numpy as np
import polars as pl
# example dataframe
rng = np.random.default_rng(1)
nrows = 3_000_000
df = pl.DataFrame(
dict(
day=rng.integers(1, 300, nrows),
id=rng.integers(1, 5_000, nrows),
id2=rng.integers(1, 5, nrows),
value=rng.normal(0, 1, nrows),
)
)
# joining df with itself takes around 10-15 seconds on a machine with 32 cores.
start = time.perf_counter()
df.join(df, on=["id", "id2"], how="left")
time.perf_counter() - start
# joining df with itself with extra conditions - the implementation below that takes very similar …Run Code Online (Sandbox Code Playgroud) 在极坐标中,我试图创建一个新列,我已经知道这些列的值,我只想添加一个包含这些值的列。下面是代码
import polars as pl
df = pl.DataFrame({"a": [1, 2], "b": [4, 5]})
english_titles = ["ok", "meh"]
df.with_columns(pl.lit(english_titles).alias("english_title"))
Run Code Online (Sandbox Code Playgroud)
但以上不起作用,我认为这是因为我使用pl.lit错误
我正在尝试在极坐标数据框中显示列的完整宽度。给定以下极坐标数据框:
\nimport polars as pl \n\ndf = pl.DataFrame({\n \'column_1\': [\'TF-IDF embeddings are done on the initial corpus, with no additional N-Gram representations or further preprocessing\', \'In the eager API, the expression is evaluated immediately. The eager API produces results immediately after execution, similar to pandas. The lazy API is similar to Spark, where a plan is formed upon execution of a query, but the plan does not actually access the data until the collect method is called to execute …Run Code Online (Sandbox Code Playgroud) 假设我们在极坐标(python)中有这个数据框:
import polars as pl
df = pl.DataFrame(
{
"era": ["01", "01", "02", "02", "03", "03"],
"pred": [3,5,6,8,9,1]
}
)
Run Code Online (Sandbox Code Playgroud)
我可以根据一列创建排名/行号,例如:
df.with_columns(rn = pl.col("era").rank("ordinal"))
Run Code Online (Sandbox Code Playgroud)
但如果我想基于两列来完成它,它就不起作用:
df.with_columns(rn = pl.col(["era","pred"]).rank("ordinal"))
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
ComputeError: The name: 'rn' passed to `LazyFrame.with_columns` is duplicate
Error originated just after this operation:
DF ["era", "pred"]; PROJECT */2 COLUMNS; SELECTION: "None"
Run Code Online (Sandbox Code Playgroud)
关于如何执行此操作有什么建议吗?
在 pandas 中,人们可以这样做:
import pandas as pd
d = {"foo":[1,2,3, None], "bar":[4,None, None, 6]}
df_pandas = pd.DataFrame.from_dict(d)
dict(df_pandas.isnull().sum())
Run Code Online (Sandbox Code Playgroud)
[出去]:
{'foo': 1, 'bar': 2}
Run Code Online (Sandbox Code Playgroud)
在极坐标中,可以通过循环列来执行相同的操作:
import polars as pl
d = {"foo":[1,2,3, None], "bar":[4,None, None, 6]}
df_polars = pl.from_dict(d)
{col:df_polars[col].is_null().sum() for col in df_polars.columns}
Run Code Online (Sandbox Code Playgroud)
使用 时,循环遍历极坐标中的列尤其痛苦LazyFrame,因此.collect()必须分块完成才能进行聚合。
有没有办法找到没有。极地数据帧中每一列中的空值而不循环遍历每一列?
假设我有:
\nIn [1]: df = pl.DataFrame({\'a\': [[1,2], [3,4]]})\n\nIn [2]: df\nOut[2]:\nshape: (2, 1)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 a \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x82\n\xe2\x94\x82 list[i64] \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 [1, 2] \xe2\x94\x82\n\xe2\x94\x82 [3, 4] \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n我知道 的所有元素\'a\'都是相同长度的列表。
我可以:
\nIn [10]: df.select([pl.col(\'a\').arr.get(i).alias(f\'a_{i}\') for i in range(2)])\nOut[10]:\nshape: (2, 2)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 a_0 \xe2\x94\x86 a_1 \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1 \xe2\x94\x86 2 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x86 4 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n但这涉及硬编码2。
有没有办法在不进行硬编码的情况下做到这一点2?我可能事先不知道列表中有多少元素(我只知道它们都有相同数量的元素)
我有一个代码
df.select([
pl.all().exclude("elapsed_time_linreg"),
pl.col("elapsed_time_linreg").arr.get(0).suffix("_slope"),
pl.col("elapsed_time_linreg").arr.get(1).suffix("_intercept"),
pl.col("elapsed_time_linreg").arr.get(2).suffix("_resid_std"),
])
Run Code Online (Sandbox Code Playgroud)
解压函数的结果
@jit
def linear_regression(session_np: np.ndarray) -> np.ndarray:
w = len(session_np)
x = np.arange(w)
sx = w ** 2 / 2
sy = np.sum(session_np)
sx2 = (w * (w + 1) * (2 * w + 1)) / 6
sxy = np.sum(x * session_np)
slope = (w * sxy - sx * sy) / (w * sx2 - sx**2)
intercept = (sy - slope * sx) / w
resids = session_np - (x * …Run Code Online (Sandbox Code Playgroud) 我需要计算两个日期列之间的所有月份结束并分解结果列表。
import polars as pl
from datetime import datetime
df = pl.DataFrame(
{
"id": ["A", "A", "A", "B", "B"],
"value": ["1", "2", "3", "4", "5"],
"valid_from": [
datetime(2020, 1, 1),
datetime(2021, 1, 1),
datetime(2022, 1, 1),
datetime(2020, 1, 1),
datetime(2021, 1, 1),
],
"valid_to": [
datetime(2020, 12, 31),
datetime(2021, 12, 31),
datetime(2022, 12, 31),
datetime(2020, 12, 31),
datetime(2021, 12, 31),
],
}
)
def __month_range(dict):
start,end = dict.values()
return pl.date_range(start, end, "1mo_saturating", eager=True).dt.month_end()
df.with_columns(
pl.struct(["valid_from","valid_to"]).apply(__month_range).alias("test")
).explode("test")
Run Code Online (Sandbox Code Playgroud)
这是这样做的方法吗?或者是否有更简单/更快的方法而不使用 struct ?
我想使用 Polars 将数据帧的值转换为数组。
对于 Pandas 我会这样做:
import pandas as pd
df_tests = pd.DataFrame({'col_a':[1,2,3], 'col_b':[4,5,6]}, 'col_c':[7,8,9]})
print(df_tests.values)
Run Code Online (Sandbox Code Playgroud)
Polars 中的等效项是什么?
我尝试过 to_list() 方法,但仅适用于系列。我能得到的最接近的是这个,它返回一个元组列表:
import polars as pl
# Convert Pandas DataFrame to Polars DataFrame
df_tests = pd.DataFrame({'col_a':[1,2,3], 'col_b':[4,5,6]}, 'col_c':[7,8,9]})
df_tests_pl = pl.from_pandas(df_tests)
print(df_tests_pl.select(pl.col(['col_a', 'col_b'])).rows())
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 C++ 读取包含浮点数列表的镶木地板文件中的数据。
\n我使用以下 python 代码生成了一个简单的镶木地板文件:
\nimport polars as pl\nimport struct\nimport random\nimport pyarrow.parquet as pq\n\nfloatlist = []\nfor _ in range(10):\n lstlen = random.choice([3, 4, 5])\n floatlist.append([random.random() for _ in range(lstlen)])\n\ndf = pl.DataFrame({"float_list": floatlist})\n\nfile_out_path = \'test.parquet\'\ndf.write_parquet(file_out_path)\nprint(pl.read_parquet(file_out_path))\nRun Code Online (Sandbox Code Playgroud)\n结果看起来非常合理:
\nshape: (10, 1)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 float_list \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x82\n\xe2\x94\x82 list[f64] \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 [0.863913, 0.831073, 0.264516] \xe2\x94\x82\n\xe2\x94\x82 [0.51377, 0.434267, \xe2\x80\xa6 0.131684] \xe2\x94\x82\n\xe2\x94\x82 [0.978071, 0.251396, \xe2\x80\xa6 0.142218] \xe2\x94\x82\n\xe2\x94\x82 [0.495616, 0.628793, 0.434872] \xe2\x94\x82\n\xe2\x94\x82 \xe2\x80\xa6 \xe2\x94\x82\n\xe2\x94\x82 [0.19035, 0.68318, \xe2\x80\xa6 0.778707] \xe2\x94\x82\n\xe2\x94\x82 [0.103636, 0.08755, \xe2\x80\xa6 0.526014] \xe2\x94\x82\n\xe2\x94\x82 [0.803863, …Run Code Online (Sandbox Code Playgroud) python-polars ×10
python ×5
pandas ×3
apache-arrow ×1
c++ ×1
dataframe ×1
date ×1
date-range ×1
datetime ×1
null ×1
numpy ×1
parquet ×1