考虑一个 Polars 数据框,其中有一列str类型指示格式中的日期'27 July 2020'。我想将此列转换为polars.datetime与 Python 标准不同的类型datetime。以下代码使用标准datetime格式,可以运行,但 Polars 不会将列中的值识别为日期。
import polars as pl
from datetime import datetime
df = pd.read_csv('<some CSV file containing a column called 'event_date'>')
df = df.with_columns([
pl.col('event_date').apply(lambda x: x.replace(" ","-"))\
.apply(lambda x: datetime.strptime(x, '%d-%B-%Y'))
])
Run Code Online (Sandbox Code Playgroud)
假设我们尝试进一步处理df以创建一个新列,指示事件发生的季度。
df = df.with_columns([
pl.col('event_date').apply(lambda x: x.month)\
.apply(lambda x: 1 if x in range(1,4) else 2 if x in range(4,7) else 3 if x in range(7,10) else 4)\
.alias('quarter') …Run Code Online (Sandbox Code Playgroud) 我想用 Polars 替换 Pandas,但我无法找到如何在不转换为 Pandas 的情况下将 Polars 与 Plotly 一起使用。我想知道是否有一种方法可以将 Pandas 完全排除在这个过程之外。
考虑以下测试数据:
import polars as pl
import numpy as np
import plotly.express as px
df = pl.DataFrame(
{
"nrs": [1, 2, 3, None, 5],
"names": ["foo", "ham", "spam", "egg", None],
"random": np.random.rand(5),
"groups": ["A", "A", "B", "C", "B"],
}
)
fig = px.bar(df, x='names', y='random')
fig.show()
Run Code Online (Sandbox Code Playgroud)
我希望这段代码在 Jupyter 笔记本中显示条形图,但它返回一个错误:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/frame.py:1483: UserWarning: accessing series as Attribute of a DataFrame is deprecated
warnings.warn("accessing series as Attribute of a DataFrame is deprecated") …Run Code Online (Sandbox Code Playgroud)