无法在Append = True的镶木地板文件中存储的Dask数据帧上使用索引

如何解决无法在Append = True的镶木地板文件中存储的Dask数据帧上使用索引

我有一个用例,我想通过to_parquet(ddf,'TestParquet',append = True)将多个Dask数据帧存储到一个公共镶木存储中。

镶木地板文件的结构是通过向其写入的第一个数据帧设置的(没有append = True)。

每个数据框都有“分类索引”列。

类别从一开始就在所有数据框中都是已知的,并且没有一个数据框具有相同的类别。

数据帧在整个类别列表上进行了分区(因此每个分区在保存到拼花地板之前都具有空分区)。最终,一旦所有daframe保存到镶木地板上,所有类别/分区将包含数据。

问题:追加第二个数据框后,索引不能用于检索特定类别。

这是一个最小的可重现示例:

熊猫:0.24.2 快速拼花:0.4.1 快手:2.22.0

import pandas as pd
import dask.dataframe as dd
import fastparquet

data= {'Name': [ 'C','B','F','B'],'ID':[1,1,2,3,3],'Value':[2,4,3]}
df = pd.DataFrame(data)
df['Name']=df['Name'].astype(pd.api.types.CategoricalDtype(categories=['A','C','D','E','F'],ordered=True) )
ddf = dd.from_pandas(df,npartitions=2)
ddf=ddf.set_index('Name',sorted=False).repartition(divisions=['A',force=True)
ddf_parquet=dd.read_parquet('./TestParquet')

data2= {'Name': ['D','A','A'],4],'Value':[1,5,6]}
df2 = pd.DataFrame(data2)
df2['Name']=df2['Name'].astype(pd.api.types.CategoricalDtype(categories=['A',ordered=True) )
ddf2 = dd.from_pandas(df2,npartitions=2)
ddf2=ddf2.set_index('Name',force=True)
dd.to_parquet(ddf2,'./TestParquet',engine='fastparquet',append=True,ignore_divisions=True)
ddf_parquet2=dd.read_parquet('./TestParquet')

将第一个数据帧保存到镶木地板后,我可以使用索引而不会出现问题:

ddf_parquet.loc['B'].head()

      ID  Value
Name
F      3      2
F      2      4

但是,在附加第二个数据帧之后,尝试选择除第一个分区的索引值('B')以外的任何内容都会导致错误:

ddf_parquet2.loc['A'].head()


Traceback (most recent call last):
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4805,in get_slice_bound
    return self._searchsorted_monotonic(label,side)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4756,in _searchsorted_monotonic
    return self.searchsorted(label,side=side)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/base.py",line 1501,in searchsorted
    return self._values.searchsorted(value,side=side,sorter=sorter)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/arrays/categorical.py",line 1370,in searchsorted
    raise ValueError("Categorical not ordered\nyou can use "
ValueError: Categorical not ordered
you can use .as_ordered() to change the Categorical to an ordered one

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/dask/dataframe/methods.py",line 42,in try_loc
    return loc(df,iindexer,cindexer)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/dask/dataframe/methods.py",line 28,in loc
    return df.loc[iindexer]
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexing.py",line 1500,in __getitem__
    return self._getitem_axis(maybe_callable,axis=axis)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexing.py",line 1867,in _getitem_axis
    return self._get_slice_axis(key,line 1533,in _get_slice_axis
    slice_obj.step,kind=self.name)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4673,in slice_indexer
    kind=kind)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4872,in slice_locs
    start_slice = self.get_slice_bound(start,'left',kind)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4808,in get_slice_bound
    raise err
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4802,in get_slice_bound
    slc = self._get_loc_only_exact_matches(label)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 4772,in _get_loc_only_exact_matches
    return self.get_loc(key)
  File "/cba/local/mx/Linux_x86_64/python/Python-3.6.2/lib/python3.6/site-packages/pandas/core/indexes/category.py",line 438,in get_loc
    raise KeyError(key)
KeyError: 'A'

我不知道什么,为什么在索引中找不到'A'键,以及为什么错误提示要对类别进行排序,因为在设置索引之前已对'名称'类别进行了排序。

一个观察结果:

  • ,两个数据帧的划分均按预期设置为:

    ('A','B','C','D','E','F','F')

保存到Parquet并读回数据后,在保存第一个数据帧后将分区重新对齐到第一个数据帧的非空分区:

('B','F')

因此,我必须在第二个Daframe后面附加ignore_partitions = True,否则我将收到一条错误消息,指出划分是重叠的(这正是为什么我在保存最终分区/之前先对整个列表类别进行了分区/从第一个数据帧开始划分。

重置索引和重新索引实际上是不可行的,因为实际数据集非常庞大(保存到Parquet之前总共约200GB)。

设置每个数据框的划分以匹配其实际类别,最终会导致相同的错误。

任何帮助将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-