3

我正在运行 Python 2.6。我有以下示例,我试图连接 csv 文件中的日期和时间字符串列。根据我设置的 dtype(None vs object),我看到了一些我无法解释的行为差异,请参阅帖子末尾的问题 1 和 2。返回的异常描述性不太强,并且 dtype 文档没有提到当 dtype 设置为 object 时预期的任何特定行为。

这是片段:

#! /usr/bin/python

import numpy as np

# simulate a csv file
from StringIO import StringIO
data = StringIO("""
Title
Date,Time,Speed
,,(m/s)
2012-04-01,00:10, 85
2012-04-02,00:20, 86
2012-04-03,00:30, 87
""".strip())


# (Fail) case 1: dtype=None splicing a column fails

next(data)                                                      # eat away the title line
header = [item.strip() for item in next(data).split(',')]       # get the headers
arr1 = np.genfromtxt(data, dtype=None, delimiter=',',skiprows=1)# skiprows=1 for the row with units
arr1.dtype.names = header                                       # assign the header to names
                                                                # so we can do y=arr['Speed']
y1 = arr1['Speed']  

# Q1 IndexError: invalid index
#a1 = arr1[:,0] 
#print a1
# EDIT1: 
print "arr1.shape " 
print arr1.shape # (3,)

# Fails as expected TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'
# z1 = arr1['Date'] + arr1['Time'] 
# This can be workaround by specifying dtype=object, which leads to case 2

data.seek(0)        # resets

# (Fail) case 2: dtype=object assign header fails
next(data)                                                          # eat away the title line
header = [item.strip() for item in next(data).split(',')]           # get the headers
arr2 = np.genfromtxt(data, dtype=object, delimiter=',',skiprows=1)  # skiprows=1 for the row with units

# Q2 ValueError: there are no fields define
#arr2.dtype.names = header # assign the header to names. so we can use it to do indexing
                         # ie y=arr['Speed']
# y2 = arr['Date'] + arr['Time']    # column headings were assigned previously by arr.dtype.names = header

data.seek(0)        # resets

# (Good) case 3: dtype=object but don't assign headers
next(data)                                                          # eat away the title line
header = [item.strip() for item in next(data).split(',')]           # get the headers
arr3 = np.genfromtxt(data, dtype=object, delimiter=',',skiprows=1)  # skiprows=1 for the row with units
y3 = arr3[:,0] + arr3[:,1]                                          # slice the columns
print y3

# case 4: dtype=None, all data are ints, array dimension 2-D

# simulate a csv file
from StringIO import StringIO
data2 = StringIO("""
Title
Date,Time,Speed
,,(m/s)
45,46,85
12,13,86
50,46,87
""".strip())

next(data2)                                                      # eat away the title line
header = [item.strip() for item in next(data2).split(',')]       # get the headers
arr4 = np.genfromtxt(data2, dtype=None, delimiter=',',skiprows=1)# skiprows=1 for the row with units
#arr4.dtype.names = header # Value error
print "arr4.shape " 
print arr4.shape # (3,3)

data2.seek(0)        # resets

问题 1:在评论 Q1 中,当 dtype=None 时,为什么我不能对列进行切片?这可以通过 a) arr1=np-genfromtxt... 像案例 3 一样使用 dtype=object 进行初始化,b) arr1.dtype.names=... 被注释掉以避免案例 2 中的值错误

问题 2:在评论 Q2 中,为什么我不能在 dtype=object 时设置 dtype.names?

编辑1:

添加了一个案例 4,如果模拟 csv 文件中的值都是整数,则显示数组的维度何时为 2-D。可以对列进行切片,但分配 dtype.names 仍然会失败。

将术语“拼接”更新为“切片”。

4

1 回答 1

2

问题 1

这是索引,而不是“拼接”,您不能索引到 的列,原因与我之前在此处回答问题 7data中向您解释的原因完全相同。看- 它是,即1D,而不是 2D。没有可供您索引的列。arr1.shape(3,)arr1

现在看看arr2- 你会看到它的形状(3,3)。为什么是这样?如果您指定,dtype=desired_type会将np.genfromtxt输入字符串的每个分隔部分视为相同(即desired_type),并且它会给您一个普通的、非结构化的 numpy 数组 back

我不太确定你想用这条线做什么:

z1 = arr1['Date'] + arr1['Time'] 

您的意思是像这样将日期和时间字符串连接在一起:'2012-04-01 00:10'?你可以这样做:

z1 = [d + ' ' + t for d,t in zip(arr1['Date'],arr1['Time'])]

这取决于你想对输出做什么(这会给你一个字符串列表,而不是一个 numpy 数组)。

我应该指出,从 1.7 版开始,Numpy 具有支持功能的核心数组类型datetime。这将允许您做更多有用的事情,例如计算时间增量等。

dts = np.array(z1,dtype=np.datetime64)

编辑: 如果要绘制时间序列数据,可以使用matplotlib.dates.strpdate2num将字符串转换为 matplotlib datenums,然后使用plot_date()

from matplotlib import dates
from matplotlib import pyplot as pp

# convert date and time strings to matplotlib datenums
dtconv = dates.strpdate2num('%Y-%m-%d%H:%M')
datenums = [dtconv(d+t) for d,t in zip(arr1['Date'],arr1['Time'])]

# use plot_date to plot timeseries
pp.plot_date(datenums,arr1['Speed'],'-ob')

您还应该看看 Pandas,它有一些用于可视化时间序列数据的好工具

问题2

您无法设置namesof,arr2因为它不是结构化数组(见上文)。

于 2013-07-19T10:00:46.797 回答