merge_asof(): cannot use tolerance flag when the index is a TimedeltaIndex · Issue #27642 · pandas-dev/pandas (original) (raw)

Code Sample

import pandas as pd import numpy as np

print( """ \nPandas merge_asof() bug:

\tUnimplemented error?
\tcannot use tolerance flag when my index is a timedelta (not a timestamp)
\tjust documenting so I can try to add this functionality
    
""")

print(f"pandas version: {pd.version}") print(f"numpy version: {np.version}")

delta_300 = pd.timedelta_range(start='0 minutes', freq='3333334 N', periods=301, name='Time') delta_120 = pd.timedelta_range(start='0 minutes', freq='8333334 N', periods=301, name='Time')

df_300hz = pd.DataFrame({'my300hz_data': np.arange(301)}, index=delta_300) df_120hz = pd.DataFrame({'my120hz_data': np.arange(301)}, index=delta_120)

print(df_300hz) print(df_120hz)

this throws error "pandas.errors.MergeError: key must be integer, timestamp or float"

merged = pd.merge_asof(df_120hz, df_300hz, on='Time', direction='nearest', tolerance=pd.Timedelta('15 ms'))

The line below works, but output is not what I want

merged = pd.merge_asof(df_120hz, df_300hz, on='Time', direction='nearest')

merged.set_index('Time', inplace=True) print(merged)

Problem description

I need to see NaNs when I merge and there is a gap in my data, without begin able to use the tolerance flag my data gets smoothed.

Current work around, convert all my TimedeltaIndex's to a time stamp. Since I do not have a date for this data I am using unix time. This feels bulky since I am going to drop the date when I save the file anyway.

Expected Output

[301 rows x 1 columns]
my120hz_data my300hz_data
Time
00:00:00 0 0
00:00:00.008333 1 2
00:00:00.016666 2 5
00:00:00.025000 3 7
00:00:00.033333 4 10
... ... ...
00:00:02.466666 296 NaN
00:00:02.475000 297 NaN
00:00:02.483333 298 NaN
00:00:02.491666 299 NaN
00:00:02.500000 300 NaN

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.0-5-amd64
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.0
numpy : 1.17.0
pytz : 2019.1
dateutil : 2.8.0
pip : 18.1
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.0
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None