(Jan-20-2023, 12:57 AM)badtwistoffate Wrote: Unfortunately, I don't think I'm doing it in a python-efficient way as the script has been executing for over three hours. Here's some example code. Is there a better way to compare time stamps and not rely as heavily on for loops?Pandas often need a tolal diffent approch than how stuff are done in Python,the
loops
are usually out 🚀Look at this blog post.
So try to work on whole dataframs(build in tools) using
vectorization
(this mean avoid Python-level loops)To give exampe how to compare time between two or more dataframs.
import pandas as pd # Create Dataframe 1 date1 = ['2022-01-01 00:00:00', '2022-01-02 12:00:00', '2022-01-03 18:00:00'] wind_speed1 = [5, 6, 7] df1 = pd.DataFrame({'date_time': pd.to_datetime(date1), 'wind_speed': wind_speed1}) # Create Dataframe 2 date2 = ['2022-01-02 08:00:00', '2022-01-03 15:00:00', '2022-01-04 20:00:00'] wind_speed2 = [8, 9, 10] df2 = pd.DataFrame({'date_time': pd.to_datetime(date2), 'wind_speed': wind_speed2}) # Convert the specific time to a 'pandas.Timestamp' object specific_time = df1.iloc[1]['date_time'].time() specific_time = pd.to_datetime(specific_time, format='%H:%M:%S').time() # Compare time less than a specific time and show the result print(df2[df2['date_time'].dt.time < specific_time])
Output: date_time wind_speed
0 2022-01-02 08:00:00 8