Python / Pandas - How to compare if column content of the data frame, in date format, is equal to another column of date plus or minus 1 day?

0

I have a date frame with two columns in the date format and I need to compare if one of them is between the interval of one more day or less than another column. For example: Column A = 2015-03-02 Column B = 2015-03-01 Comparation: If Column A == Column B:     Return 'Column A within range' Elif (Column B - 1) == Column A:     Return 'Column within range' Elif (Column B + 1) == Column A:     Return 'Column A within range' Else:     Return 'Column A OFF the accepted range'

    
asked by anonymous 13.07.2017 / 17:33

1 answer

2

Hello, just a suggestion, to write code use the StackOverflow tool itself for this. It stays in the {} icon of the text editor.

To do this, you can use the pd.to_datetime().day method and request the day directly.

I'm considering that you're already using pandas to manipulate the DataFrame and that column_A and column_B are only values of DataFrame cells.

coluna_A = '2015-03-02' 
coluna_B = '2015-03-01'
A = coluna_A.to_datetime().day
B = coluna_B.to_datetime().day

def comparacao( A , B):
    if (A <= B+1 and A >= B-1): 
        return 'Coluna A dentro do intervalo' 
    else: 
        return 'Coluna A FORA do intervalo aceito'

comparacao(A,B)
    
14.07.2017 / 06:27