3

Python 2.6 を実行しています。次の例では、csv ファイルから日付と時刻の文字列列を連結しようとしています。設定した dtype (None と 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 を設定できないのはなぜですか?

EDIT1:

シミュレートされた csv ファイルの値がすべて int の場合に配列の次元が 2 次元になる場合を示すケース 4 を追加しました。列をスライスすることはできますが、dtype.names の割り当ては失敗します。

「スプライス」という用語を「スライス」に更新します。

4

1 に答える 1