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')] 

올바른 형식으로 보입니다. 그래서 난 내 DF에 다시 넣어보십시오

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

그리고 오류 얻을 : 나는 무엇을 변경해야

TypeError('data type not understood',) 

를? 고맙습니다. 최근에 아나콘다를 업데이트하기 전에이 작업이 진행 중이었습니다. 문제를 인식하지 못했습니다. 감사.

조정 :

df.dtype 내가 문자열에 '0'이하 7 개 문자를 변경할 수있는 방법

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 - 이건 유니 코드 문제입니까? 유니 코드와 :

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에서 유니 코드 이름을 사용할 수 있으며,이 작업을해야합니다.
큰 점 중 하나는 Py2의 경우 Numpy가 튜플 목록으로 유니 코드 필드 이름을 사용하여 dtype을 지정할 수 없지만 사전을 사용하는 것이 허용됩니다.내가 Py2에서 유니 코드 이름을 사용하지 않는 경우

, 나는 |S7|0에서 마지막 필드를 변경하거나 유니 코드 문자열로 이름을 정의하면 encode("ascii")을 사용해야합니다.


과 관련된 버그

...

은 당신이 무엇을보고 일어나는 이유를 이해하기 위해서는 NumPy와와 팬더와에보고 된 버그/문제 좀보고 유용 상대적 토론.

NumPy와
https://github.com/numpy/numpy/issues/2407
당신은 (내가 여기에보고하지 않는) 일의 주로 몇 토론에 알 수 있습니다

  • 은 "문제는"잠시
  • 동안 진행되었습니다
  • 사람들이 사용하는 속임수는 유니 코드 문자열에 을 사용하는 것이 었습니다.
  • 'whatever' 문자열의 기본값 (바이트/유니크)이 다릅니다. ode) in Py2/3
  • @hpaulj 자신이 의 문제 보고서에서 아름답게 주석 처리했습니다. "dtype 사양이 튜플 유형 목록 인 경우 각 이름이 문자열 (py2 또는 3에 정의 된대로)인지 확인합니다. DTYPE 사양 사전 {'names':[ alist], 'formats':[alist]...} 경우, py2 케이스는 유니 이름 "

판다 허용 NumPy와 문제에 관한 문제가보고되어있다 팬더 측에서도
: https://github.com/pandas-dev/pandas/pull/13462
그것을 오래 전에 고정되어있는 것처럼 보입니다.

관련 문제