Numpy dtype - data type incomprehensible

I have a dataframe that I am looking at the data types associated with each column.

When I run:

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

I want to set dtype currency on S7. I do:

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

It looks like the correct format. Therefore, I am trying to return it to my df:

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

And I get the error message:

TypeError('data type not understood',)

What should I change? Thank. This worked before I recently updated anaconda and I am not aware of this issue. Thank.

ADJUSTMENT:

df.dtype

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

How to change "0" to a string less than 7 characters?

How can I change the last dtype from 'O' to something else? In particular, the string is less than 7 characters.

Is LASTLY a problem with unicode? With 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

No Unicode:

In [39]: np.dtype([('date', '<i8')])
Out[39]: dtype([('date', '<i8')])
+6
source share
2 answers

, , , , .

.

dtypes , :

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

OBJ , 2 3. ( descr __array_interface__ .)

, field_name, ( '' , 'f #'). 2- , "" ( Unicode), , , "", Python. , field_dtype, , . field_shape , . , 3- , 1, 2-. dtype, , .

, , , , , , , ((u'date', 'date'), '<i8'), "" ( , !), .
, , ((u'date', u'date'), '<i8') .

Unicode Py2, encode("ascii")

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

.
, Py2 Numpy dtype dtype , .

Unicode Py2, |0 |S7 encode("ascii") Unicode.


...

, , , /, Numpy Pandas, .

Numpy
https://github.com/numpy/numpy/issues/2407
( ) :

  • ""
  • , , encode("ascii")
  • , 'whatever' 'whatever' (/) Py2/3
  • @hpaulj , " dtype , , ( py2 3). dtype {'names':[ alist], 'formats':[alist]...} format {'names':[ alist], 'formats':[alist]...}, py2 "


, numpy: https://github.com/pandas-dev/pandas/pull/13462
, .

+7

NumPy. . numpy :

pip install --upgrade - numpy

0

Source: https://habr.com/ru/post/1686061/


All Articles