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

Asked

Viewed 1,465 times

1

I have a date frame with two columns in date format and I need to compare if one of them is between the interval of one day more or less than another column. For example: Column A = 2015-03-02 Column B = 2015-03-01 Comparison: 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 OUT of ACCEPTED range'

1 answer

3

Hello, just a hint, to write code use the Stackoverflow tool itself for this. It is in the icon {} text editor.

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

I’m considering you’re already using the pandas to manipulate the Dataframe and that coluna_A and coluna_B are only values of the 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.