Pandas unit testing: how to assert equality of NaT and NaN values?

In NumPy and Pandas, nan != nanand NaT != NaT. So, comparing the results during unit testing, how can I say that the return value is one of these values? Simple assertEqualnaturally fails even if I use pandas.util.testing.

+4
source share
3 answers

Before you run the assert_frame_equal check, you can use the method . fillna () for the data to replace the null values ​​with something else that won. Otherwise, it will not appear in your values. You can also read these examples on how to use the .fillna () method.

+1
source

If you are comparing scalars, one way is to use assertTruewith isnull. For example, in UnitFrame ( pandas/tests/test_frame.py) unit tests, you can find tests like this :

self.assertTrue(com.isnull(df.ix['c', 'timestamp']))

(It comis an alias for pandas/core/common.py, therefore, it com.isnullcalls the same main function as pd.isnull.)

, , DataFrames , tm.assert_series_equal tm.assert_frame_equal. :

>>> import pandas.util.testing as tm
>>> df = pd.DataFrame({'a': [1, np.nan]})
>>> df
    a
0   1
1 NaN

NaN NaN:

>>> df == df
       a
0   True
1  False

assert_frame_equal NaN :

>>> tm.assert_frame_equal(df, df)
# no AssertionError raised
+5

Testing in python2.7, I get the following

import numpy as np
import pandas as pd

x = np.nan
x is np.nan #True
x is pd.NaT #False
np.isnan(x) #True
pd.isnull(x) #True

y = pd.NaT
y is np.nan #False
y is pd.NaT #True
np.isnan(y) #TypeError !!
pd.isnull(y) #True

You can also use

x != x #True for nan
y != y #True for NaT

But I do not like this style, I can never convince myself of this.

+3
source

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


All Articles