python - Matlab 或 IDL 用户可以处理 Python 日期时间对象吗?

标签 python datetime pandas hdf5

不幸的是,我无法检查这一点,因为我没有 IDL 或 Matlab 许可证,这就是我在这里询问的原因。

场景是我用 pandas 保存 HDF5 文件,其中一列是 Python 日期时间对象。 IDL 和 Matlab 有很好的 HDF5 阅读库(至少我听说过),但是 IDL/Matlab 用户能够对 HDF5 文件中找到的日期时间对象做任何有用的事情吗?

最佳答案

In [28]: df = DataFrame({ 'A' : np.random.rand(5), 
                          'B' : range(5), 
                          'C' : date_range('20130101',periods=5,freq='T')})

In [29]: df
Out[29]: 
          A  B                   C
0  0.067509  0 2013-01-01 00:00:00
1  0.872840  1 2013-01-01 00:01:00
2  0.379634  2 2013-01-01 00:02:00
3  0.552827  3 2013-01-01 00:03:00
4  0.996150  4 2013-01-01 00:04:00

[5 rows x 3 columns]

In [30]: df.dtypes
Out[30]: 
A           float64
B             int64
C    datetime64[ns]
dtype: object

写出表格格式。

In [32]: df.to_hdf('test.h5','df',mode='w',format='table')

显示文件的内部结构

In [33]: !ptdump -avd test.h5
/ (RootGroup) ''
  /._v_attrs (AttributeSet), 4 attributes:


n [32]: df.to_hdf('test.h5','df',mode='w',format='table')

In [33]: !ptdump -avd test.h5
/ (RootGroup) ''
  /._v_attrs (AttributeSet), 4 attributes:
   [CLASS := 'GROUP',
    PYTABLES_FORMAT_VERSION := '2.1',
    TITLE := '',
    VERSION := '1.0']
/df (Group) ''
  /df._v_attrs (AttributeSet), 14 attributes:
   [CLASS := 'GROUP',
    TITLE := '',
    VERSION := '1.0',
    data_columns := [],
    encoding := None,
    index_cols := [(0, 'index')],
    info := {1: {'type': 'Index', 'names': [None]}, 'index': {}},
    levels := 1,
    nan_rep := 'nan',
    non_index_axes := [(1, ['A', 'B', 'C'])],
    pandas_type := 'frame_table',
    pandas_version := '0.10.1',
    table_type := 'appendable_frame',
    values_cols := ['values_block_0', 'values_block_1', 'values_block_2']]
/df/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
  "values_block_1": Int64Col(shape=(1,), dflt=0, pos=2),
  "values_block_2": Int64Col(shape=(1,), dflt=0, pos=3)}
  byteorder := 'little'
  chunkshape := (2048,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
  /df/table._v_attrs (AttributeSet), 19 attributes:
   [CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'values_block_0',
    FIELD_2_FILL := 0,
    FIELD_2_NAME := 'values_block_1',
    FIELD_3_FILL := 0,
    FIELD_3_NAME := 'values_block_2',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer',
    values_block_0_dtype := 'float64',
    values_block_0_kind := ['A'],
    values_block_1_dtype := 'int64',
    values_block_1_kind := ['B'],
    values_block_2_dtype := 'datetime64',
    values_block_2_kind := ['C']]
  Data dump:
[0] (0, [0.06750856214219292], [0], [1356998400000000000])
[1] (1, [0.8728395428343044], [1], [1356998460000000000])
[2] (2, [0.37963409103250334], [2], [1356998520000000000])
[3] (3, [0.5528271410494643], [3], [1356998580000000000])
[4] (4, [0.9961498806897623], [4], [1356998640000000000])

datetime64[ns] 被序列化为自 UTC 纪元以来的纳秒,并存储为 int64 列类型(这与 numpy 存储基础数据相同)。因此阅读起来非常简单,因为它是标准 HDF5 格式。但是,您需要解释元数据。请参阅 pandas/io/pytables.py 中的源文件。

基本上,您会寻找datetime64类型 block (该类型映射这些库的名称)。然后你可以在IDL/matlab中进行反向转换(在pandas中你会做pd.to_datetime(ns_since_epoch,unit='ns')。时区有点棘手,因为值是UTC,而时区存储在 info 属性中。

注意:对于固定格式的元数据的解释或如果您有data_columns(但不是很难做到),这略有不同。

关于python - Matlab 或 IDL 用户可以处理 Python 日期时间对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22191216/

相关文章:

python m2crypt 和 load_key,但我只有一个 .cert 文件

Eclipse 中的 Python 文档

python - 使用opencv在图像上编写表情符号

c# - 如何在 Dynamics CRM 中将日期时间字段转换为日期?

python - 在 Pandas 中使用带有元组列的查询

python - Eurostat 的 pandas 数据挖掘

python - 大内存 Python 后台作业

python - 显示开始和结束之间的年份

c# - 将无效日期处理为有效日期?

python - 比较 Pandas 列中的上一个和下一个不同值