How can I check the date time is ok with no date in the data frame?

I have a pandas dataframe with 4 columns containing null int values ​​and some dates. I want to create a new column with a True / False value to tell me if the date times are chronologically aligned, even though some are 0.

Df example

P1.   P2.   P3.   P4. 
0.    2011.  0.   2015
2015. 0.    0.    2013

      

Then I want to create a new column that only has T / F, depending on whether it is in chronological order, eg. in the case above, line 1 is true and line 2 is incorrect.

I looked into a loop with if and else, but I was wondering if there is a cleaner way. FYI dates are in full format 2014-11-31 00:00:00.

Thanks in advance.

+3


source to share


2 answers


DF source:

In [250]: x
Out[250]:
      P1.     P2.  P3.   P4.
0     0.0  2011.0  0.0  2015
1  2015.0     0.0  0.0  2013

      

Decision:



In [251]: x['new'] = x[x!=0].apply(lambda x: x.dropna().is_monotonic, axis=1)

      

Result:

In [252]: x
Out[252]:
      P1.     P2.  P3.   P4.    new
0     0.0  2011.0  0.0  2015   True
1  2015.0     0.0  0.0  2013  False

      

+2


source


Here is the method following the logic:

  • Use the method apply

    to loop through the line axis = 1;
  • For each line, remove the zeros, calculate the difference, and compare with the zeros;
  • If none of the differences are less than zero, you can claim to be in chronological order (ascending);


Plus, if the string contains zero or one valid date (zero), this logic gives true:

df.apply(lambda r: ~(r[r != 0].diff() < 0).any(), 1)

#0     True
#1    False
#dtype: bool

      

+2


source







All Articles