有没有办法强制镶木地板文件将pd.DataFrame列编码为给定类型,即使该列的所有值都为空?镶木地板在其模式中自动分配"null"的事实阻止我将许多文件加载到单个文件中dask.dataframe.
试图使用pandas列投射df.column_name = df.column_name.astype(sometype)不起作用.
我为什么这么问
我想将许多镶木地板文件加载到一个单独的dask.dataframe.所有文件都是pd.DataFrame使用多个实例生成的df.to_parquet(filename).所有数据帧都具有相同的列,但对于某些列,给定列可能只包含空值.当试图将所有文件加载到dask.dataframe(使用时df = dd.read_parquet('*.parquet'),我得到以下错误:
Schema in filename.parquet was different.
id: int64
text: string
[...]
some_column: double
vs
id: int64
text: string
[...]
some_column: null
Run Code Online (Sandbox Code Playgroud)
重现我的问题的步骤
import pandas as pd
import dask.dataframe as dd
a = pd.DataFrame(['1', '1'], columns=('value',))
b = pd.DataFrame([None, None], columns=('value',))
a.to_parquet('a.parquet')
b.to_parquet('b.parquet')
df = dd.read_parquet('*.parquet') # Reads a and b
Run Code Online (Sandbox Code Playgroud)
这给了我以下内容:
ValueError: Schema in path/to/b.parquet was different. …Run Code Online (Sandbox Code Playgroud) 我正在将 Pandas 与名为 的 DataFrame 一起使用df。我正在用它提取新功能,并将生成的两个新数据帧与pd.concat. 这是我的功能:
def get_processed_df(df, rare_cols, threshold=10):
print("df at start", df.shape)
df = df[pd.notnull(df["FullDescription"]) &
pd.notnull(df["Title"]) &
pd.notnull(df["SalaryNormalized"])]
print("df after filtering nulls", df.shape)
tfidf_desc = get_tfidf_df(df,
"FullDescription",
max_features=100,
prefix="DESC",
tokenize=tokenize)
print("tfidf_desc shape: ", tfidf_desc.shape)
tfidf_title = get_tfidf_df(df,
"Title",
max_features=100,
prefix="TITLE",
tokenize=tokenize)
print("tfidf_title shape: ", tfidf_title.shape)
df.drop("FullDescription", inplace=True, axis=1)
df.drop("Title", inplace=True, axis=1)
final_df = pd.concat([df, tfidf_desc, tfidf_title], axis=1)
print("final df shape: ", final_df.shape)
return final_df
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出:
df at start (10000, 12)
df after …Run Code Online (Sandbox Code Playgroud)