0

I have a big dataframe of datetime and values so I will just be taking a sample dataframe called df1

DateTime            Values
2020-12-26 14:00:00 439.0
2020-12-26 14:00:00 441.0
2020-12-26 14:00:00 461.0
2020-12-27 08:00:00 536.0
2020-12-27 08:00:00 547.0
2020-12-27 08:00:00 484.0
2020-12-27 08:00:00 497.0
2020-12-27 14:00:00 491.0
2020-12-27 14:00:00 512.0
2020-12-27 14:00:00 529.0
2020-12-27 14:00:00 436.0

and I have another full dataframe df 2

DateTime            Trend   
2020-12-18 14:00    no trend    
2020-12-19 08:00    no trend    
2020-12-19 14:00    no trend    
2020-12-21 08:00    no trend        
2020-12-21 14:00    no trend    
2020-12-22 08:00    no trend    
2020-12-22 14:00    decreasing  
2020-12-23 08:00    no trend    
2020-12-23 14:00    no trend    
2020-12-24 08:00    no trend    
2020-12-24 14:00    no trend    
2020-12-25 08:00    no trend    
2020-12-25 14:00    decreasing  
2020-12-26 08:00    no trend
2020-12-26 14:00    decreasing  
2020-12-27 08:00    no trend    
2020-12-27 14:00    no trend    

I am trying to create a new column df1[Trend] and assign the trend values based on the right date time

The result should be like this

DateTime            Values    Trend
2020-12-26 14:00:00 439.0     decreasing
2020-12-26 14:00:00 441.0     decreasing
2020-12-26 14:00:00 461.0     decreasing
2020-12-27 08:00:00 536.0     no trend
2020-12-27 08:00:00 547.0     no trend
2020-12-27 08:00:00 484.0     no trend
2020-12-27 08:00:00 497.0     no trend
2020-12-27 14:00:00 491.0     no trend
2020-12-27 14:00:00 512.0     no trend
2020-12-27 14:00:00 529.0     no trend
2020-12-27 14:00:00 436.0     no trend

Is there anyway I could it?

1 Answer 1

1

Use pandas.DataFrame.merge:

df1.merge(df2)

              DateTime  Values       Trend
0  2020-12-26 14:00:00   439.0  decreasing
1  2020-12-26 14:00:00   441.0  decreasing
2  2020-12-26 14:00:00   461.0  decreasing
3  2020-12-27 08:00:00   536.0    no trend
4  2020-12-27 08:00:00   547.0    no trend
5  2020-12-27 08:00:00   484.0    no trend
6  2020-12-27 08:00:00   497.0    no trend
7  2020-12-27 14:00:00   491.0    no trend
8  2020-12-27 14:00:00   512.0    no trend
9  2020-12-27 14:00:00   529.0    no trend
10 2020-12-27 14:00:00   436.0    no trend

EDIT

You're getting

ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat

because df1["DateTime"] is a datetime column but df2["DateTime"] is a str column then you can't merge. To fix it convert df2["DateTime"] to datetime with pandas.to_datetime:

df2['DateTime'] = pd.to_datetime(df2.DateTime)
Sign up to request clarification or add additional context in comments.

5 Comments

I got this error when I do it. ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat However by doing pd.concat if will just combine two dataframes horizontally and result in the remaining to have nan values which is not what I am looking for\
Convert into datetime columndf2['DateTime'] = pd.to_datetime(df2.DateTime).
@DDM check it now :P
Thanks alot yes its due to the object type
@DDM Nice! happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.