5

After doing element wise multiplication in pandas I end up with a negative zero value in one of the dataframe elements and a regular zero value in another dataframe element. Python indicates that they are equal yet displays them differently. On what level are they not equal and how will that affect calculations later on?

In[100]:
import numpy as np
import pandas as pd

In[101]:
df_a = pd.DataFrame([[-5.2,3.1,2.8],[1,2,3],[4,5,4]], columns=['Col0','Col1','Col2'], index=['Row0','Row1','Row2'])
df_b = pd.DataFrame([[0,0,2],[1,2,3],[4,5,4]], columns=['Col0','Col1','Col2'], index=['Row0','Row1','Row2'])
df_c = df_a * df_b

In[102]: print df_a
Out[102]:
      Col0  Col1  Col2
Row0  -5.2   3.1   2.8
Row1   1.0   2.0   3.0
Row2   4.0   5.0   4.0

In[103]: print df_b
Out[103]:
      Col0  Col1  Col2
Row0     0     0     2
Row1     1     2     3
Row2     4     5     4

In[104]: print df_c
Out[104]:
      Col0  Col1  Col2
Row0    -0     0   5.6
Row1     1     4   9.0
Row2    16    25  16.0

In[105]:
negative_zero = df_c.iloc[0,0]
positive_zero = df_c.iloc[0,1]
print(negative_zero == positive_zero)

Out[105]:
True

In[106]: print(type(negative_zero))
Out[106]: <type 'numpy.float64'>

In[107]: print(type(positive_zero))
Out[107]: <type 'numpy.float64'>
Gaspare Bonventre
  • 1,134
  • 11
  • 19

1 Answers1

5

-0 is equal to 0. The signed zero should not effect any further calculations.

You can check out Wikipedia, the IEEE-standard and various other articles on stackoverflow about this topic.

Klaster
  • 673
  • 1
  • 7
  • 17