Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create Pandas DF with one week of random dates...
- dates1 = pd.date_range(start='2017-09-01', end='2017-09-07', freq='5s')
- # Create DataFrame...
- Z = [('Date',dates1)]
- ZZ = pd.DataFrame.from_items(Z)
- # Add random ID column for later reference...
- ZZ['ID'] = np.random.choice(range(1,500), ZZ.shape[0])
- # Take first sample...
- df1 = ZZ.sample(200)
- # Create a list of Unique dates...
- uniquedates1 = list(df1['Date'].dt.normalize().unique())
- # Take second sample...
- df2 = ZZ.sample(200)
- uniquedates2 = list(df2['Date'].dt.normalize().unique())
- # Find the intersection (shared dates)
- sd = set(uniquedates1) & set(uniquedates2)
- shareddates = list(sd)
- # Merge on Date
- merged = {}
- for date in shareddates:
- print(date)
- # Select whole day
- # Create date selection mask using one date and timedelta addition
- mask = (df1['Date'] > date) & (df1['Date'] <= date + np.timedelta64(24, 'h'))
- df11 = df1.loc[mask]
- mask = (df2['Date'] > date) & (df2['Date'] <= date + np.timedelta64(24, 'h'))
- df22 = df2.loc[mask]
- merged[date] = df11.merge(df22, how='outer', left_on='Date', right_on='Date')
- merged = {}
- for date in shareddates:
- print(date)
- # Select whole day
- # Create date selection mask using one date and timedelta addition
- mask = (df1['Date'] > date) & (df1['Date'] <= date + np.timedelta64(24, 'h'))
- df11 = df1.loc[mask]
- # Resample to minutes
- df11.index = df11['Date']
- del df11['Date']
- df111 = df11.resample('T')
- mask = (df2['Date'] > date) & (df2['Date'] <= date + np.timedelta64(24, 'h'))
- df22 = df2.loc[mask]
- # Resample to minutes
- df22.index = df22['Date']
- del df22['Date']
- df222 = df22.resample('T')
- merged[date] = df1111.merge(df222, how='outer', left_index=True, right_index=True)
- # ValueError: can not merge DataFrame with instance of type <class 'pandas.core.resample.DatetimeIndexResampler'>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement