Search code examples
pythonpandastimedifference

Add only to negative days in dataframe columns


I have a dataframe where I obtained a difference column between two different hours (below an example):

    difference 
    00:30:15 
    00:15:00
    -1 days +02:27:00
    -1 days +00:41:00

those values are marked as timedelta type. So how can I add one day only for those negative values (to turn positive the difference)? I have tried to do:

mask=df[df["difference"]<"0"]
df[mask,"difference"]=df["difference"] + pd.Timedelta(days=1)

(quote marks for zero because otherwise dont accept it, dont know why)

output:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

and then I try:

df.loc[df["difference"]<0, "difference"]=df["difference"] + pd.Timedelta(days=1)

output:

TypeError: Invalid comparison between dtype=timedelta64[ns] and int

So, I dont know what else can be done. I have reviewed a lot of questions here but none has helped me so far. Any ideas are welcomed.


Solution

  • You were almost there. mask should be a boolean array, not a dataframe:

    mask=df["difference"]<"0" # << Note the change
    df.loc[mask,"difference"]=df["difference"] + pd.Timedelta(days=1)
    print(df)
    

    Output:

      difference
    0   00:30:15
    1   00:15:00
    2   02:27:00
    3   00:41:00