2017-09-20 1 views
4

私は各列に関連付けられたデータ型を見ているデータフレームを持っています。Numpy dtype - データ型が理解されていない

私が実行します。

In [23]: df.dtype.descr 

Out [24]: [(u'date', '<i8'), (u'open', '<f8'), (u'high', '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', '|O')] 

私はS7に通貨DTYPEを設定します。私はやっている:

In [25]: dtype_new[-1] = (u'currency', "|S7") 
In [26]: print dtype_new 
Out [27]: [(u'date', '<i8'), (u'open', '<f8'), (u'high', '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', '|S7')] 

これは正しい形式に見えます。

In [28]: df = df.astype(np.dtype(dtype_new)) 

そして、私はエラーを取得:だから、私はDFに戻って、それを入れてみてください

TypeError('data type not understood',) 

は、私が何を変えるべきか?ありがとうございました。これは、私が最近anacondaを更新する前に動作していて、私はその問題を認識していません。ありがとう。

ADJUSTMENT:

df.dtypeは私が7文字未満の文字列に '0' を変更するにはどうすればよい

In [23]: records.dtype 
Out[23]: dtype((numpy.record, [(u'date', '<i8'), (u'open', '<f8'), (u'high',  '<f8'), (u'low', '<f8'), (u'close', '<f8'), (u'volume', '<f8'), (u'dividend', '<f8'), (u'adj_factor', '<f8'), (u'split_factor', '<f8'), (u'liq', '<f8'), (u'currency', 'O')])) 

のですか?

最後のdtypeを 'O'から別のものに変更するにはどうすればよいですか?具体的には、7文字未満の文字列です。

LASTLY - これはユニコードの問題ですか? Unicodeを使用した :

In [38]: np.dtype([(u'date', '<i8')]) 
    ...: 
    --------------------------------------------------------------------------- 
TypeError         Traceback (most recent call  last) 
<ipython-input-38-8702f0c7681f> in <module>() 
----> 1 np.dtype([(u'date', '<i8')]) 

TypeError: data type not understood 

ませんユニコード:

In [39]: np.dtype([('date', '<i8')]) 
Out[39]: dtype([('date', '<i8')]) 
+0

これは 'np.ones(1、dtype = df.dtype)'と一緒に使えます。問題がこのフィールドの値にあるのだろうかと思います。また、私はPy3を使用しています。 – hpaulj

+0

私はpy2上にいます - numpyのアップグレードが実行される前に動作していました。 – user1911092

+4

最小の実用例を制作することを検討しましたか?また、どのような正確なバージョンを実行していますか? – norok2

答えて

6

実際に、あなたが痛い点に触れているように見える、あなたはユニコードについてのポイントを中心にしているようだと。

最後のnumpyのドキュメントから始めましょう。

ドキュメントdtypes状態:

[(field_name, field_dtype, field_shape), ...]

obj should be a list of fields where each field is described by a tuple of length 2 or 3. (Equivalent to the descr item in the __array_interface__ attribute.)

The first element, field_name , is the field name (if this is '' then a standard field name, 'f#', is assigned). The field name may also be a 2-tuple of strings where the first string is either a “title” (which may be any string or unicode string) or meta-data for the field which can be any object, and the second string is the “name” which must be a valid Python identifier. The second element, field_dtype , can be anything that can be interpreted as a data-type. The optional third element field_shape contains the shape if this field represents an array of the data-type in the second element. Note that a 3-tuple with a third argument equal to 1 is equivalent to a 2-tuple. This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description.

だからドキュメントが実際にフィールド名がユニコード可能かどうかを指定していないようだ、我々はドキュメントから確認することができるものであること、私たちはタプルを定義した場合フィールド名として、例えば((u'date', 'date'), '<i8')、ユニコードを「タイトル」として使用すると(注意しても、まだ名前のためではありません!)、エラーは発生しません。
この場合も、((u'date', u'date'), '<i8')と定義するとエラーになります。

さて、あなたはencode("ascii")

(u'date'.encode("ascii")) 

を使用してPY2でのUnicode名を使用することができ、これは動作するはずです。
大きな点の1つは、Py2の場合、Numpyはユニコードのフィールド名を持つタプルのリストとしてdtypeを指定することを許可していませんが、辞書を使用することができます。私はPY2でユニコード名を使用しない場合は

、私は|0から|S7に最後のフィールドを変更することができたり、あなたがUnicode文字列として名を定義する場合encode("ascii")を使用する必要があります。


と関わるバグ...

あなたが見る何が起こる理由を理解することは、numpyのとパンダに報告されたバグ/問題を見て、持っていると便利です相対的な議論。

numpyの
https://github.com/numpy/numpy/issues/2407
あなたは、物事の主カップル(私はここで報告していないもの)の議論に気づくことができる:「問題は」しばらくの間続いている

  • ユニークな文字列の上でencode("ascii")を使用することが1つのトリックの人々でした。
  • 'whatever'の文字列には異なるデフォルト値(バイト/ユニコードode)Py2/3
  • @hpaulj自身がこの問題の報告でその旨を美しくコメントした"dtype仕様がタプルタイプのリストである場合、各名前が文字列(py2または3で定義されている)かどうかを確認します。 DTYPE仕様は辞書{'names':[ alist], 'formats':[alist]...}であれば、PY2ケースもユニコード名」

パンダを可能 numpyの問題に関連する問題が報告されているパンダの側にも
https://github.com/pandas-dev/pandas/pull/13462
それをずっと前に固定されているようだ。

関連する問題