- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
最终的解决方案是使用 read_csv 的“converters”参数并在将其添加到 DataFrame 之前检查每个值。最终80多GB的原始数据只有2个破值。
参数看起来像这样:
converters={'XXXXX': self.parse_xxxxx}
像这样的小型静态辅助方法:
@staticmethod
def parse_xxxxx(input):
if not isinstance(input, float):
try:
return float(input)
except ValueError:
print "Broken Value: ", input
return float(0.0)
else:
return input
在尝试阅读 ca 时。将 40GB 以上的 csv 数据放入 HDF 文件中 我遇到了一个令人困惑的问题。读取大约 1GB 后,整个过程失败并出现以下错误
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 658, in append
self._write_to_group(key, value, table=True, append=True, **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 923, in write_to_group
s.write(obj = value, append=append, complib=complib, **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 2985, in write **kwargs)
File "/usr/lib/python2.7/dist-packages/pandas/io/pytables.py", line 2675, in create_axes
raise ValueError("cannot match existing table structure for [%s] on appending data" % items)
ValueError: cannot match existing table structure for [Date] on appending data
我使用的 read_csv 调用如下:
pd.io.parsers.read_csv(filename, sep=";|\t", compression='bz2', index_col=False, header=None, names=['XX', 'XXXX', 'Date', 'XXXXX'], parse_dates=[2], date_parser=self.parse_date, low_memory=False, iterator=True, chunksize=self.input_chunksize, dtype={'Date': np.int64})
当我明确地将 dtype 设置为 int64 时,为什么新 block 的“日期”列不适合现有列?
感谢您的帮助!
下面是解析日期的函数:
@staticmethod
def parse_date(input_date):
import datetime as dt
import re
if not re.match('\d{12}', input_date):
input_date = '200101010101'
timestamp = dt.datetime.strptime(input_date, '%Y%m%d%H%M')
return timestamp
在遵循 Jeff 的一些提示后,我可以提供有关我的问题的更多详细信息。这是我用来加载 bz2 编码文件的完整代码:
iterator_data = pd.io.parsers.read_csv(filename, sep=";|\t", compression='bz2', index_col=False, header=None,
names=['XX', 'XXXX', 'Date', 'XXXXX'], parse_dates=[2],
date_parser=self.parse_date, iterator=True,
chunksize=self.input_chunksize, dtype={'Date': np.int64})
for chunk in iterator_data:
self.data_store.append('huge', chunk, data_columns=True)
self.data_store.flush()
csv 文件遵循以下模式:{STRING};{STRING};{STRING}\t{INT}
为输出文件调用的 ptdump -av 的输出如下:
ptdump -av datastore.h5
/ (RootGroup) ''
/._v_attrs (AttributeSet), 4 attributes:
[CLASS := 'GROUP',
PYTABLES_FORMAT_VERSION := '2.0',
TITLE := '',
VERSION := '1.0']
/huge (Group) ''
/huge._v_attrs (AttributeSet), 14 attributes:
[CLASS := 'GROUP',
TITLE := '',
VERSION := '1.0',
data_columns := ['XX', 'XXXX', 'Date', 'XXXXX'],
encoding := None,
index_cols := [(0, 'index')],
info := {'index': {}},
levels := 1,
nan_rep := 'nan',
non_index_axes := [(1, ['XX', 'XXXX', 'Date', 'XXXXX'])],
pandas_type := 'frame_table',
pandas_version := '0.10.1',
table_type := 'appendable_frame',
values_cols := ['XX', 'XXXX', 'Date', 'XXXXX']]
/huge/table (Table(167135401,), shuffle, blosc(9)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"XX": StringCol(itemsize=16, shape=(), dflt='', pos=1),
"XXXX": StringCol(itemsize=16, shape=(), dflt='', pos=2),
"Date": Int64Col(shape=(), dflt=0, pos=3),
"XXXXX": Int64Col(shape=(), dflt=0, pos=4)}
byteorder := 'little'
chunkshape := (2340,)
autoIndex := True
colindexes := {
"Date": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"index": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XXXX": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XXXXX": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
"XX": Index(6, medium, shuffle, zlib(1)).is_CSI=False}
/huge/table._v_attrs (AttributeSet), 23 attributes:
[XXXXX_dtype := 'int64',
XXXXX_kind := ['XXXXX'],
XX_dtype := 'string128',
XX_kind := ['XX'],
CLASS := 'TABLE',
Date_dtype := 'datetime64',
Date_kind := ['Date'],
FIELD_0_FILL := 0,
FIELD_0_NAME := 'index',
FIELD_1_FILL := '',
FIELD_1_NAME := 'XX',
FIELD_2_FILL := '',
FIELD_2_NAME := 'XXXX',
FIELD_3_FILL := 0,
FIELD_3_NAME := 'Date',
FIELD_4_FILL := 0,
FIELD_4_NAME := 'XXXXX',
NROWS := 167135401,
TITLE := '',
XXXX_dtype := 'string128',
XXXX_kind := ['XXXX'],
VERSION := '2.6',
index_kind := 'integer']
经过大量额外的调试后,我遇到了以下错误:
ValueError: invalid combinate of [values_axes] on appending data [name->XXXX,cname->XXXX,dtype->int64,shape->(1, 10)] vs current table [name->XXXX,cname->XXXX,dtype->string128,shape->None]
然后我尝试通过添加修改 read_csv 调用来修复此问题,以便为 XXXX 列强制使用正确的类型,但我收到了相同的错误:
dtype={'XXXX': 's64', 'Date': dt.datetime})
是 read_csv 忽略了 dtype 设置还是我在这里遗漏了什么?
当读取 block 大小为 10 的数据时,最后 2 个 chunk.info() 调用给出以下输出:
Int64Index: 10 entries, 0 to 9
Data columns (total 4 columns):
XX 10 non-null values
XXXX 10 non-null values
Date 10 non-null values
XXXXX 10 non-null values
dtypes: datetime64[ns](1), int64(1), object(2)<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 4 columns):
XX 10 non-null values
XXXX 10 non-null values
Date 10 non-null values
XXXXX 10 non-null values
dtypes: datetime64[ns](1), int64(2), object(1)
我使用的是 pandas 版本 0.12.0。
最佳答案
好的,你有几个问题:
当指定要传递给read_csv
的数据类型时,它们必须是 numpy 数据类型;和字符串 dtypes 被转换为 object
dtype(所以 s64
不做任何事情)。 datetime
也没有,这就是 parse_dates
的用途。
不同 block 中的 dtype 是不同的,即第一个 block 中有 2 个 int64
列和 1 个 object
,而第二个 block 中有 1 个 int64
和 2 个 object
。这是你的问题。 (我认为错误消息可能有点令人困惑,IIRC 在更高版本的 pandas 中已修复)。
因此,您需要使每个 block 中的数据类型都相同。您可能在该特定列中有混合数据。一种方法是指定 dtype = { column_that_is_bad : 'object' }
。另一种方法是在该列上使用 convert_objects(convert_numeric=True)
将所有非数字值强制转换为 nan
(这也会将列的 dtype 更改为 float64
).
关于python - 附加到 HDFStore 失败,返回 "cannot match existing table structure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770260/
我将所有数据都放入了 HDFStore(是的!),但是如何从中取出数据.. 我在我的 HDFStore 中保存了 6 个数据帧作为 frame_table。这些表格中的每一个看起来如下所示,但长度各不
我确定这可能非常简单,但我无法弄清楚如何通过日期时间索引对 Pandas HDFStore 表进行切片以获得特定范围的行。 我有一个看起来像这样的表: mdstore = pd.HDFStore(st
我有一个 pandas HDFStore,我尝试从中进行选择。我想在一个大的 np.array 中选择两个时间戳之间的数据和一个 id。以下代码可以工作,但仅在查询列表中的成员身份时才会占用过多内存。
我对 pandas' HDFStore 有一些问题速度太慢了,不幸的是我无法从这里的其他问题中找到令人满意的解决方案。 情况 我有一个很大的 DataFrame,其中大部分包含 float ,有时包含
我对 pandas' HDFStore 有一些问题速度太慢了,不幸的是我无法从这里的其他问题中找到令人满意的解决方案。 情况 我有一个很大的 DataFrame,其中大部分包含 float ,有时包含
清楚我做错了什么吗? 我正在尝试 pandas HDFStore.select start 和 stop 选项,但没有什么区别。 我使用的命令是: import pandas as pd hdf =
我希望将我读入的两个表存储在数据框中。 我正在将 h5 文件读入我的代码中: with pd.HDFStore(directory_path) as store: self.df = stor
我在平面文件中有数 TB 的数据(在子集中),我想使用 Python Pandas/Pytables/H5py 将这些数据转换为 HDF5 以加快查询和搜索速度。我计划使用 to_hdf 之类的方法转
我有一个 pandas HDFStore,我试图从中进行选择。我想在一个大的 np.array 中选择两个带有 id 的时间戳之间的数据。以下代码有效,但仅在查询列表中的成员资格时占用过多内存。如果我
问题:如何创建一个允许对 pandas HDFStore 对象中的多个列进行迭代的生成器? 我正在尝试为 pandas HDFStore 对象创建一个包装类。我试图实现的功能之一是能够按给定的 blo
我在具有多索引的 HDFStore 中将frame_table 称为“数据”。在 DataFrame 中,它可能看起来像这样 var1 var2 va
以下代码片段: HDFStore = pandas.io.pytables.HDFStore lock = threading.RLock() with lock:
Pandas "Group By" Query on Large Data in HDFStore? 我已经尝试了答案中的示例,只是我希望能够按两列进行分组。 基本上,修改代码看起来像 with pd
我通过 Pandas 将大量数据帧导出到一系列 HDFStore 文件。我需要能够根据需要快速提取每个数据帧的最新记录。 设置: File path: /data/storage_X100.hdf
这很好用: cols = ['X', 'Y'] ind = [('A', 1), ('B', 2)] ind = pd.MultiIndex.from_tuples(index, names=['fo
假设我有一个 store = pd.HDFStore('cache/cache.h5') 我有一个存储的 DataFrame store['myDF'] 如果在我的代码中,我这样做: a = stor
我是 pytables 的新手,对存储压缩的 pandas DataFrame 有疑问。我当前的代码是: import pandas # HDF5 file name H5name="C:\\MyDi
我想知道为什么 HDFStore 会在 pandas 中的字符串列上发出警告。我认为它可能是我真实数据库中的 NaN,但在这里尝试它会给我两个列的警告,即使一个没有混合并且只是字符串。 使用 .13.
我正在试验不同的 pandas 友好存储方案来存储报价数据。迄今为止最快的(就读取和写入而言)是使用具有 blosc 压缩和“固定”格式的 HDFStore。 store = pd.HDFStore(
我有以下 DataFrame,它作为名为数据的 frame_table 存储在 HDFStore 对象中: shipmentid qty catid
我是一名优秀的程序员,十分优秀!