Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge_asof(): cannot use tolerance flag when the index is a TimedeltaIndex #27642

Closed
ianzur opened this issue Jul 29, 2019 · 4 comments · Fixed by #27650

Comments

@ianzur
Copy link
Contributor

commented Jul 29, 2019

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

@mroeschke

This comment has been minimized.

Copy link
Member

commented Jul 29, 2019

merge_asof should have raised a NotImplementedError with TimedeltaIndex, but I think it's reasonable to support it for that data type.

@ianzur

This comment has been minimized.

Copy link
Contributor Author

commented Jul 29, 2019

I think adding an elif statement to handle timedeltaIndex then verify tolerance is positive gives the result I desire.

            elif is_timedelta64_dtype(lt):
                if not isinstance(self.tolerance, Timedelta):
                    raise MergeError(msg)
                if self.tolerance < Timedelta(0):
                    raise MergeError("tolerance must be positive")

line 1665 merge.py

This does give the results I desire, so not unimplemented?
I haven't tried to break it though.

also add "is_timedelta64_dtype" to from pandas.core.dtypes.common import ()

or use is_datetimelike() or is_datetime_or_timedelta_dtype() for if check?

@ianzur

This comment has been minimized.

Copy link
Contributor Author

commented Jul 29, 2019

Still works after I add gaps to my 300Hz data.

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.show_versions()}")
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)

df_300hz.drop(df_300hz.index[10:110], inplace=True)

print(df_300hz)
print(df_120hz) 

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

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

This comment has been minimized.

Copy link
Member

commented Jul 29, 2019

Right, you can just add the is_timedelta64_dtype check here:

if is_datetime64_dtype(lt) or is_datetime64tz_dtype(lt):

PR's welcome!

ianzur added a commit to ianzur/pandas that referenced this issue Jul 29, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Jul 29, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Jul 29, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Jul 30, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Jul 31, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Aug 8, 2019
ianzur added a commit to ianzur/pandas that referenced this issue Aug 9, 2019
TomAugspurger added a commit to ianzur/pandas that referenced this issue Aug 20, 2019
TomAugspurger added a commit to ianzur/pandas that referenced this issue Aug 22, 2019

@TomAugspurger TomAugspurger added this to the 0.25.1 milestone Aug 22, 2019

TomAugspurger added a commit that referenced this issue Aug 22, 2019
BUG: timedelta merge asof with tolerance (#27650)
* issue #27642 - timedelta merge asof with tolerance
meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this issue Aug 22, 2019
TomAugspurger added a commit that referenced this issue Aug 22, 2019
galuhsahid added a commit to galuhsahid/pandas that referenced this issue Aug 25, 2019
BUG: timedelta merge asof with tolerance (pandas-dev#27650)
* issue pandas-dev#27642 - timedelta merge asof with tolerance
gabriellm1 added a commit to gabriellm1/pandas that referenced this issue Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.