Most efficient way to find the second closest date
Tag : mysql , By : arbeitandy
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You would change the LIMIT 1 to LIMIT 1 OFFSET 1. Note: OFFSET 0 means gets the first row. So, OFFSET 1 means get the second row.
|
Merge 2 dataframes on closest past date
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have 2 dataframes with column 0 for 'Date'. , Here's a try with merge_asof: df = pd.merge_asof( main.set_index('Date').sort_index(),
sec.set_index('Date',drop=False).sort_index(),
left_index=True,
right_index=True,
direction='backward') # backward is the default, so you
# can leave this out if you prefer
df.rename(columns={'Date':'Date_sec'})\
.sort_index(ascending=False).reset_index()
Date Date_sec
0 2016-07-26 2016-07-01
1 2016-04-26 2016-04-01
2 2016-01-26 2016-01-01
3 2015-10-27 2015-10-01
4 2015-07-21 2015-07-01
5 2015-04-27 2015-04-01
6 2015-01-27 2015-01-01
7 2014-10-20 2014-10-01
8 2014-07-22 2014-07-01
9 2014-04-23 2014-04-01
10 2014-01-27 2014-01-01
11 2013-10-28 2013-10-01
12 2013-07-23 2013-07-01
|
find closest rows between dataframes with positive timedelta
Tag : python , By : user185751
Date : March 29 2020, 07:55 AM
Any of those help write a function to get the closest index & timestamp in df_short given a timestamp def get_closest(n):
mask = df_short.mytime_short >= n
ids = np.where(mask)[0]
if ids.size > 0:
return ids[0], df_short.mytime_short[ids[0]]
else:
return np.nan, np.nan
df = df_long.mytime_long.apply(get_closest)
df
# output:
0 (0, 2013-01-10 00:00:02)
1 (2, 2013-01-10 00:00:06)
2 (nan, nan)
df = pd.merge_asof(df_long,
df_short.reset_index(),
left_on='mytime_long',
right_on='mytime_short',
direction='forward')[['index', 'mytime_short']]
df
# output:
index mytime_short
0 0.0 2013-01-10 00:00:02
1 2.0 2013-01-10 00:00:06
2 NaN NaT
|
Merging dataframes by closest earlier date by group
Date : March 29 2020, 07:55 AM
should help you out I have two dataframes generated by the following code: , You can using merge_asof with by and on parameter df_usages.date=pd.to_datetime(df_usages.date)
df_logins.date=pd.to_datetime(df_logins.date)
df_usages,df_logins=df_usages.sort_values('date').rename(columns={'date':'use_date'}),df_logins.sort_values('date').rename(columns={'date':'log_date'})
pd.merge_asof(df_usages,df_logins,left_on='use_date',right_on='log_date',by='id',direction = 'nearest')
Out[168]:
use_date id log_date
0 2013-09-20 13:02:00 1 2013-09-20 13:01:00
1 2013-09-20 13:02:00 2 2013-09-20 13:01:00
2 2013-09-20 13:05:00 1 2013-09-20 13:05:00
3 2013-09-20 13:05:00 2 2013-09-20 13:05:00
4 2013-09-20 13:06:00 1 2013-09-20 13:05:00
5 2013-09-20 13:06:00 2 2013-09-20 13:05:00
6 2013-09-20 13:35:00 1 2013-09-20 13:37:00
7 2013-09-20 13:35:00 2 2013-09-20 13:37:00
8 2013-09-20 13:38:00 1 2013-09-20 13:37:00
9 2013-09-20 13:38:00 2 2013-09-20 13:37:00
10 2013-09-20 13:45:00 1 2013-09-20 13:45:00
11 2013-09-20 13:45:00 2 2013-09-20 13:45:00
12 2013-09-20 13:57:00 1 2013-09-20 13:59:00
13 2013-09-20 13:57:00 2 2013-09-20 13:59:00
|
Find closest point in Pandas DataFrames
Tag : python , By : christiandsg
Date : March 29 2020, 07:55 AM
|