0

I have an issue multiplying 2 dataframes as follows with the same number of columns and rows. Both are filled with a float64 data type. It output an empty dataframe. Any contribution could help, please.

pos dataframe:

          2019-03-01    2019-03-02    2019-03-03    
currency                                                                                                                                  
1WO       2.600000e+02  2.600000e+02  2.600000e+02  
ADH       8.219730e+02  8.219730e+02  8.219730e+02

c_price dataframe:

timestamp      2019-03-01  2019-03-02  2019-03-03  
currency                                                                                                                                                       
1WO            1.8140      1.7230      1.7250      
ADH            0.2425      0.2465      0.2387

my code:

jpy_bal = pos * c_price 

df.columns.dtype output for both df:

object

df.dtypes output for both df:

2019-03-01    float64
2019-03-02    float64
2019-03-03    float64
2019-03-04    float64
2019-03-05    float64
           ...   
2019-11-01    float64
2019-11-02    float64
2019-11-03    float64
2019-11-04    float64
2019-11-05    float64
Length: 250, dtype: object
mama dede
  • 360
  • 1
  • 12
  • What is `print (pos.columns.dtype) print (c_price.columns.dtype)` ? – jezrael Nov 06 '19 at 08:28
  • @jezrael I think you have posted something but it is gone. It helped me. It should be here. – mama dede Nov 06 '19 at 08:42
  • hmmm, all things what are in my mind are in answer... – jezrael Nov 06 '19 at 08:44
  • @jezrael I though this could be dtype too at first. But look my edit for dtype check. It is the same for both df. This output told me the problem is not dtype, but it was as your solution works. What is the reasoning here? – mama dede Nov 06 '19 at 08:48
  • So if use `pos.columns = pd.to_datetime(pos.columns) c_price.columns = pd.to_datetime(c_price.columns)` it help? – jezrael Nov 06 '19 at 08:51
  • @jezrael Yes it helped. I am just trying to understand why it was the dtype. The dtype check is same output for both df... – mama dede Nov 06 '19 at 08:53
  • hmmm, are same types `print (type(pos.columns[0])) print (type(c_price.columns[0]))` ? – jezrael Nov 06 '19 at 08:55
  • @jezrael This is the output – mama dede Nov 06 '19 at 09:15
  • So it is reason, need same - both dates or both datetimes. – jezrael Nov 06 '19 at 09:16
  • @jezreal Ok, if my understanding is good. All the dtype has to be the same. Not only values but also columns labels and index. Correct? – mama dede Nov 06 '19 at 09:25
  • exactly, but because objects should be different, then have to match types – jezrael Nov 06 '19 at 09:27
  • it is same principe like mentioned [here](https://stackoverflow.com/questions/42672552/pandas-cast-column-to-string-does-not-work/42672574#42672574) - same dtypes objects, but different types. – jezrael Nov 06 '19 at 09:29

2 Answers2

0

You can do it like this:

jpy_bal = pd.DataFrame(pos.values * c_price.values, columns=pos.columns, index=pos.index)
zipa
  • 27,316
  • 6
  • 40
  • 58
0

I think there are different dtypes in columns:

print (pos.columns.dtype) 
print (c_price.columns.dtype)

So need convert them to same dtype, here DatetimeIndex in columns:

pos.columns = pd.to_datetime(pos.columns)
c_price.columns = pd.to_datetime(c_price.columns)
jpy_bal = pos * c_price

Another problem should be different index values - maybe trailing whitespaces:

pos.index = pod.index.str.strip()
c_price.index = c_price.index.str.strip()

Another idea is multiple by numpy array:

jpy_bal = pos * c_price.values 

Or:

jpy_bal = pos.values * c_price
print (jpy_bal)
     2019-03-01  2019-03-02  2019-03-03
1WO  471.640000  447.980000  448.500000
ADH  199.328452  202.616344  196.204955
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252