Skip to content Skip to sidebar Skip to footer

Merge Dataframe Object And Timedelta64

I have a dataframe of dtype datetime64 df: time timestamp 18053.401736 2019-06-06 09:38:30+00:00 18053.418252 2019-06-06 10:02:17+00:00 18053.424514 2019-06-06 10:1

Solution 1:

Convert the timestamp to timedelta by subtracting the date part and then merge:

df1 = pd.DataFrame([pd.Timestamp('2019-06-06 09:38:30+00:00'),pd.Timestamp('2019-06-06 10:02:17+00:00')], columns=['timestamp'])
df2 = pd.DataFrame([pd.Timedelta('09:38:30')], columns=['ref_time'])
    timestamp                  
0   2019-06-06 09:38:30+00:00
1   2019-06-06 10:02:17+00:00

timestamp    datetime64[ns, UTC]
dtype: object

    ref_time
0   09:38:30

ref_time    timedelta64[ns]
dtype: object
df1['merge_key'] = df1['timestamp'].dt.tz_localize(None) - pd.to_datetime(df1['timestamp'].dt.date)
df_merged = df1.merge(df2, left_on = 'merge_key', right_on = 'ref_time')

Gives:

    timestamp                   merge_key   ref_time
0   2019-06-06 09:38:30+00:00   09:38:30    09:38:30

Solution 2:

The main challenge here is to get everything into compatible date types. Using your, slightly modified, examples as inputs

from io import StringIO
df = pd.read_csv(StringIO(
"""
time,timestamp
18053.401736,2019-06-06 09:38:30+00:00
18053.418252,2019-06-06 10:02:17+00:00
18053.424514,2019-06-06 10:11:18+00:00
18053.454132,2019-06-06 10:53:57+00:00
"""))
df['timestamp'] = pd.to_datetime(df['timestamp'])

from datetime import timedelta
sdf = pd.read_csv(StringIO(
"""
ref_time
0 days 09:00:00
0 days 09:00:01
0 days 09:00:02
0 days 09:00:03
0 days 09:00:04
0 days 09:38:30
0 days 10:02:17
0 days 14:59:56
0 days 14:59:57
0 days 14:59:58
0 days 14:59:59
0 days 15:00:00
"""))
sdf['ref_time'] = pd.to_timedelta(sdf['ref_time'])

The dtypes here are as in your question which is important

First we figure out the base_date as we need to convert timedeltas into datetimes etc. Note we set it to midnight of the relevant date via round('1d')

base_date = df['timestamp'].iloc[0].round('1d').to_pydatetime()
base_date

output

datetime.datetime(2019, 6, 6, 0, 0, tzinfo=<UTC>)

Next we add timedeltas from sdf to the base_date:

sdf['ref_dt'] = sdf['ref_time'] + base_date

Now sdf['ref_dt'] and df['timestamp'] are in the same 'units' and of the same type, so we can merge

sdf.merge(df, left_on = 'ref_dt', right_on = 'timestamp', how = 'left')

output

    ref_time         ref_dt                        time  timestamp
--  ---------------  -------------------------  -------  -------------------------
 0  0 days 09:00:00  2019-06-06 09:00:00+00:00    nan    NaT
 1  0 days 09:00:01  2019-06-06 09:00:01+00:00    nan    NaT
 2  0 days 09:00:02  2019-06-06 09:00:02+00:00    nan    NaT
 3  0 days 09:00:03  2019-06-06 09:00:03+00:00    nan    NaT
 4  0 days 09:00:04  2019-06-06 09:00:04+00:00    nan    NaT
 5  0 days 09:38:30  2019-06-06 09:38:30+00:00  18053.4  2019-06-06 09:38:30+00:00
 6  0 days 10:02:17  2019-06-06 10:02:17+00:00  18053.4  2019-06-06 10:02:17+00:00
 7  0 days 14:59:56  2019-06-06 14:59:56+00:00    nan    NaT
 8  0 days 14:59:57  2019-06-06 14:59:57+00:00    nan    NaT
 9  0 days 14:59:58  2019-06-06 14:59:58+00:00    nan    NaT
10  0 days 14:59:59  2019-06-06 14:59:59+00:00    nan    NaT
11  0 days 15:00:00  2019-06-06 15:00:00+00:00    nan    NaT

and we see the merge happening where needed


Post a Comment for "Merge Dataframe Object And Timedelta64"