0

我目前正在探索 Py-Polars,在其数据框中获取 Date32 格式时遇到了一些困难。我尝试了以下方法:

  1. 直接从 Pandas 转换为 PyPolars
import pandas as pd
import pypolars as pyp

a = pd.read_csv(*CSV File*)
b = pyp.from_pandas(a)

错误代码如下:

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    pyp.from_pandas(a)
  File "C:\Users\*Username*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypolars\functions.py", line 235, in from_pandas
    pl_s = Series(k, s, nullable=True).cast(datatypes.Date64)
  File "C:\Users\*Username*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypolars\series.py", line 783, in cast
    return wrap_s(f())
RuntimeError: Any(ArrowError(ComputeError("Casting from Int32 to Date64 not supported")))
  1. 在 Pandas 中将 DateTime 转换为 String,转换为 PyPolars,在 PyPolars 中将 String 转换为 DateTime
def changeDateTime(value):
    return str(value)

a["ACTUAL_DROP_DATE"] = a["ACTUAL_DROP_DATE"].apply(changeDateTime)
a["ACTUAL_END_DATE"] = a["ACTUAL_END_DATE"].apply(changeDateTime)

b = pyp.from_pandas(a)

def changeStrBack(value):
    if value == np.str("NaT"):
        return ""
    else:
        year = int(value[0:4])
        month = int(value[5:7])
        day = int(value[8:10])
        return pyp.datetime(year, month, day)

b["ACTUAL_DROP_DATE"] = b["ACTUAL_DROP_DATE"].apply(changeStrBack, dtype_out = pyp.Date32)
b["ACTUAL_END_DATE"] = b["ACTUAL_END_DATE"].apply(changeStrBack, dtype_out = pyp.Date32)

这在转换时给了我所有的空值。(即两列完全为空)。

希望任何人对我如何在 PyPolars 中获取日期时间的列有一些想法。

谢谢!

4

0 回答 0