0

I am trying to multiply the values in two separate columns

The type of the elements in the first column is 'numpy.float64'

That of the second column is 'float'

When I do

new_column = df['first_column'] * df['second column']

I get

'TypeError: can't multiply sequence by non-int of type 'float'

I don't really understand why I cannot multiply the values of numpy.float64 and float. Aren't they similar to each other and multipliable?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Jun Jang
  • 2,101
  • 8
  • 22
  • 43
  • This question explains how to convert numpy types to native python types: https://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types – Jack Parkinson Jul 05 '17 at 18:15
  • You're not trying to multiply the values of `numpy.float64` and `float`, that's the problem. I'm guessing the first set of values is a `list` and not a numpy array? – Mark Ransom Jul 05 '17 at 18:16
  • Mark. The types of both first and second set of values are 'pandas.core.series.Series.' – Jun Jang Jul 05 '17 at 18:20
  • You could certainly use `zip` to go through both series with a list comprehension and multiply each element individually. I'm not familiar with pandas so I don't know if there are other options. – Mark Ransom Jul 05 '17 at 18:22
  • can you provide a sample dataframe which reproduces error. – shivsn Jul 05 '17 at 18:23
  • We need more information about the `df`. Isn't there some sort of `info` or `dtypes` command in `pandas`? There's something about the 2 `df` columns that doesn't match your description. – hpaulj Jul 05 '17 at 20:58

2 Answers2

2

I can reproduce your error message with:

In [267]: [1,2,3]*3.43
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-267-fc9c3bc4b243> in <module>()
----> 1 [1,2,3]*3.43

TypeError: can't multiply sequence by non-int of type 'float'

In Python (not numpy or pandas), a list or other sequence times an integer replicates the sequence:

In [268]: [1,2,3]*3
Out[268]: [1, 2, 3, 1, 2, 3, 1, 2, 3]

If

df['first_column'] * df['second column']

is producing the error, then one term is a sequence (e.g. list) the other a float. Another possibility is that one is a object dtype array, and contains one or more lists.

In [271]: np.array([(2,3),(3,)])*3
Out[271]: array([(2, 3, 2, 3, 2, 3), (3, 3, 3)], dtype=object)
In [272]: np.array([(2,3),(3,)])*3.34
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-272-c3152ad55f88> in <module>()
----> 1 np.array([(2,3),(3,)])*3.34

TypeError: can't multiply sequence by non-int of type 'float'

It could even be a mix of floats and lists, doing number * on the numbers and replication on the lists.

In [283]: np.array([(2,3),(3,),12])*np.array([[3],[2]])
Out[283]: 
array([[(2, 3, 2, 3, 2, 3), (3, 3, 3), 36],
       [(2, 3, 2, 3), (3, 3), 24]], dtype=object)

More likely it's an object array (or data series) with a mix of numbers and strings:

In [287]: np.array(['astring',12],dtype=object)*np.array([[3]])
Out[287]: array([['astringastringastring', 36]], dtype=object)
In [288]: np.array(['astring',12],dtype=object)*np.array([[3.23]])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-288-5a02408d1a73> in <module>()
----> 1 np.array(['astring',12],dtype=object)*np.array([[3.23]])

TypeError: can't multiply sequence by non-int of type 'float'
hpaulj
  • 175,871
  • 13
  • 170
  • 282
0

You could try unsafe casting like so:

numpy.multiply(A, B, out=A, casting='unsafe')
Salmaan P
  • 601
  • 1
  • 6
  • 25