我正在尝试借助分类功能和惰性 API 来连接两个 Dataframe。我尝试按照用户指南中描述的方式进行操作(https://pola-rs.github.io/polars-book/user-guide/performance/strings.html)
count = admin_df.groupby(['admin','EVENT_DATE']).pivot(pivot_column='FIVE_TYPE',values_column='count').first().lazy()
fatalities = admin_df.groupby(['admin','EVENT_DATE']).pivot(pivot_column='FIVE_TYPE',values_column='FATALITIES').first().lazy()
fatalities = fatalities.with_column(pl.col("admin").cast(pl.Categorical))
count = count.with_column(pl.col("admin").cast(pl.Categorical))
admin_df = fatalities.join(count,on=['admin','EVENT_DATE']).collect()
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Traceback (most recent call last):
File "country_level.py", line 33, in <module>
country_level('/c/Users/Sebastian/feast/fluent_sunfish/data/ACLED_geocoded.parquet')
File "country_level.py", line 10, in country_level
country_df=aggregate_by_date(df)
File "country_level.py", line 29, in aggregate_by_date
admin_df = fatalities.join(count,on=['admin','EVENT_DATE']).collect()
File "/home/sebastian/.local/lib/python3.8/site-packages/polars/internals/lazy_frame.py", line 293, in collect
return pli.wrap_df(ldf.collect())
RuntimeError: Any(ValueError("joins on categorical dtypes can only happen if they are created under the same global string cache"))
Run Code Online (Sandbox Code Playgroud)
使用with pl.StringCache():一切都可以正常工作,尽管用户指南说如果您使用惰性 …
df.filter(pl.col("MyDate") >= "2020-01-01")
Run Code Online (Sandbox Code Playgroud)
不像 pandas 那样工作。
我找到了解决方法
df.filter(pl.col("MyDate") >= pl.datetime(2020,1,1))
Run Code Online (Sandbox Code Playgroud)
但如果我需要使用字符串变量,这并不能解决问题。
在 Polars 0.13.14 中,我可以创建一个DataFrame带有全常数列的列,如下所示:
import polars as pl\n\npl.DataFrame(dict(x=pl.repeat(1, 3)))\n\n# shape: (3, 1)\n# \xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n# \xe2\x94\x82 x \xe2\x94\x82\n# \xe2\x94\x82 --- \xe2\x94\x82\n# \xe2\x94\x82 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\xa1\n# \xe2\x94\x82 1 \xe2\x94\x82\n# \xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n# \xe2\x94\x82 1 \xe2\x94\x82\n# \xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n# \xe2\x94\x82 1 \xe2\x94\x82\n# \xe2\x94\x94\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但在 Polars 0.13.15 中,这是一个错误
\nValueError: Series constructor not called properly.\nRun Code Online (Sandbox Code Playgroud)\n如何用极坐标值填充列?
\n我想在 Python 中尝试极坐标,所以我想做的就是连接从 json 读取的几个数据帧。当我将索引更改为date并查看时,lala1.head()我发现该列date消失了,所以我基本上丢失了索引。是否有更好的解决方案或者我需要按日期排序,这基本上与将索引设置为相同date?
import polars as pl
quarterly_balance_df = pl.read_json('../AAPL/single_statements/1985-09-30-quarterly_balance.json')
q1 = quarterly_balance_df.lazy().with_column(pl.col("date").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df = q1.collect()
q2 = quarterly_balance_df.lazy().with_column(pl.col("fillingDate").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df = q2.collect()
q3 = quarterly_balance_df.lazy().with_column(pl.col("acceptedDate").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df = q3.collect()
quarterly_balance_df2 = pl.read_json('../AAPL/single_statements/1986-09-30-quarterly_balance.json')
q1 = quarterly_balance_df2.lazy().with_column(pl.col("date").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df2 = q1.collect()
q2 = quarterly_balance_df2.lazy().with_column(pl.col("fillingDate").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df2 = q2.collect()
q3 = quarterly_balance_df2.lazy().with_column(pl.col("acceptedDate").str.strptime(pl.Date, "%Y-%m-%d"))
quarterly_balance_df2 = q3.collect()
lala1 = pl.from_pandas(quarterly_balance_df.to_pandas().set_index('date'))
lala2 = pl.from_pandas(quarterly_balance_df.to_pandas().set_index('date'))
test = pl.concat([lala1,lala2])
Run Code Online (Sandbox Code Playgroud) 在 pandas 中,我们有pandas.DataFrame.select_dtypes根据 .pandas 文件选择某些列的方法dtype。在 Polars 中是否有类似的方法来做这样的事情?
给定以下数据框,是否有某种方法可以仅选择以给定前缀开头的列?我知道我可以这样做pl.col(column) for column in df.columns if column.startswith("prefix_"),但我想知道我是否可以将其作为单个表达式的一部分来完成。
df = pl.DataFrame(
{"prefix_a": [1, 2, 3], "prefix_b": [1, 2, 3], "some_column": [3, 2, 1]}
)
df.select(pl.all().<column_name_starts_with>("prefix_"))
Run Code Online (Sandbox Code Playgroud)
这可以懒惰地做吗?
我正在考虑在解析问题polars中使用 in 代替numpy,将结构化文本文件转换为字符表并在不同的列上进行操作。然而,这似乎比我执行的大多数操作polars慢大约 5 倍。numpy我想知道为什么会出现这种情况,以及考虑到应该polars更快,我是否做错了什么。
例子:
import requests
import numpy as np
import polars as pl
# Download the text file
text = requests.get("https://files.rcsb.org/download/3w32.pdb").text
# Turn it into a 2D array of characters
char_tab_np = np.array(file.splitlines()).view(dtype=(str,1)).reshape(-1, 80)
# Create a polars DataFrame from the numpy array
char_tab_pl = pl.DataFrame(char_tab_np)
# Sort by first column with numpy
char_tab_np[np.argsort(char_tab_np[:,0])]
# Sort by first column with polars
char_tab_pl.sort(by="column_0")
Run Code Online (Sandbox Code Playgroud)
使用%%timeitin 时Jupyter …
我一直在使用 Polars,但它似乎缺乏 pandas 那样的 qcut 功能。
我不确定原因,但是使用当前可用的 Polars 功能是否可以达到与 pandas qcut 相同的效果?
下面显示了一个关于我可以使用 pandas qcut 做什么的示例。
import pandas as pd
data = pd.Series([11, 1, 2, 2, 3, 4, 5, 1, 2, 3, 4, 5])
pd.qcut(data, [0, 0.2, 0.4, 0.6, 0.8, 1], labels=['q1', 'q2', 'q3', 'q4', 'q5'])
Run Code Online (Sandbox Code Playgroud)
结果如下:
0 q5
1 q1
2 q1
3 q1
4 q3
5 q4
6 q5
7 q1
8 q1
9 q3
10 q4
11 q5
dtype: category
Run Code Online (Sandbox Code Playgroud)
所以,我很好奇如何使用极坐标得到相同的结果?
感谢您的帮助。
我的 CSV 文件大小为 70 GB。我想在惰性模式下加载 DF 并计算行数。最好的方法是什么?
据我所知,根据文档,在惰性模式下没有像 shape 这样的函数。我发现这个答案提供了一个不基于 Polars 的解决方案,但我想知道是否也可以在 Polars 中做到这一点。
如何按特定顺序对行进行排序
df = pl.DataFrame({"currency": ["EUR","EUR","EUR","USD","USD","USD"], "alphabet": ["A","B","C","A","B","C"]})
Run Code Online (Sandbox Code Playgroud)
我需要按货币和自定义字母顺序降序
预计会是这样的
| 货币 | 字母 |
|---|---|
| 美元 | C |
| 美元 | A |
| 美元 | 乙 |
| 欧元 | C |
| 欧元 | A |
| 欧元 | 乙 |