Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: conversion from Series to Decimal is not supported

Any ideas on how to convert a series (column) from float to decimal? I am on Python 3.6. I have read the Decimal documentation, but it offers no help.

df['rate'].dtype
Out[158]: dtype('float64')
Decimal(df['rate'])
Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\site-packages\IPython
   \core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-159-88710e11f7cd>", line 1, in <module>
    Decimal(df['rate'])
 TypeError: conversion from Series to Decimal is not supported
like image 846
Minsky Avatar asked Nov 13 '17 14:11

Minsky


People also ask

How do I convert all columns to float in pandas?

Alternatively, you can convert all string columns to float type using pandas. to_numeric() . For example use df['Discount'] = pd. to_numeric(df['Discount']) function to convert 'Discount' column to float.

How do you convert a decimal to a float in Python?

There is two methods: float_number = float ( decimal_number ) float_number = decimal_number * 1.0.

How do you convert an object to int or float in Python?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.


1 Answers

You can't cast like this, you will need to do

df['rate'] = df['rate'].apply(Decimal)

pandas does support Decimal but you can't cast like that

Example:

In[28]:
from decimal import *
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df['a'] = df['a'].apply(Decimal)
df

Out[28]: 
                                                   a         b         c
0  -1.6122557830197199457700207858579233288764953... -1.865243 -0.836893
1  0.96962430214434858211092205237946473062038421... -0.105823 -0.842267
2  -0.9113389075755260471112251252634450793266296... -0.351389 -0.183489
3  1.22765470106414120721183280693367123603820800... -1.232627 -0.067909
4  -0.0376339704393285762185072940155805554240942... -0.445606 -0.080623

the dtype will still show object but the dtype really is Decimal:

In[29]:
type(df['a'].iloc[0])

Out[29]: decimal.Decimal

If you use astype(Decimal) it will look like it worked but it doesn't:

In[38]:
df['b'].astype(Decimal)

Out[38]: 
0    -1.86524
1   -0.105823
2   -0.351389
3    -1.23263
4   -0.445606
Name: b, dtype: object

If we try to assign this back:

In[39]:
df['b'] = df['b'].astype(Decimal)
type(df['b'].iloc[0])

Out[39]: float

As pointed out by @JonClements and I agree it is ill-advised to use non-native numpy types as you lose any vectorisation in particular with arithmetic operations, additionally the dtype may be converted when you perform some operation on it which then loses your original intention

like image 116
EdChum Avatar answered Sep 29 '22 23:09

EdChum