pydata / pydata/xarray

Comparing against datetime.datetime and pd.Timestamp

Open
#4,917 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Not sure if exactly bug and what performance implications there are but it'd be more user friendly if supported:

1.) comparing against datetime

import datetime
import pandas as pd
import xarray as xr
ds = xr.Dataset(coords={'int': [0, 1, 2]})
ds['data'] = ('int', [0, 5, 6])
ds.coords['time'] = ('int', pd.date_range('2017-02-01', '2017-02-03'))
ds = ds.where(ds['time'] > datetime.datetime(2017, 2, 2))
ds

TypeError: '>' not supported between instances of 'int' and 'datetime.datetime'

2.) pd.Timestamp

import datetime
import pandas as pd
import xarray as xr
ds = xr.Dataset(coords={'int': [0, 1, 2]})
ds['data'] = ('int', [0, 5, 6])
ds.coords['time'] = ('int', pd.date_range('2017-02-01', '2017-02-03'))
ds = ds.where(ds['time'] > pd.to_datetime('2017-02-02'))
ds

This works though when converting to np.datetime64

import datetime
import pandas as pd
import xarray as xr
ds = xr.Dataset(coords={'int': [0, 1, 2]})
ds['data'] = ('int', [0, 5, 6])
ds.coords['time'] = ('int', pd.date_range('2017-02-01', '2017-02-03'))
ds = ds.where(ds['time'] > pd.to_datetime(['2017-02-02']).values)
ds

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two datetime comparison examples and the working np.datetime64 example from the issue. Trace the comparison and where-clause path used by ds['time'] > the scalar values. Done means datetime.datetime and pd.Timestamp comparisons work as shown without breaking the existing np.datetime64 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.