polars icon indicating copy to clipboard operation
polars copied to clipboard

Comparing `Date` and non-date-like string should fail - it does not

Open mkleinbort-ic opened this issue 2 years ago • 2 comments

Polars version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of Polars.

Issue description

When comparing pl.Date entries to non-date-like strings a result is given - I think this should raise an error

Reproducible example

import polars as pl 
from datetime import datetime
df = pl.DataFrame({
        'myDate' : datetime(2023,1,1),
        'fromDate' : datetime(2022,1,1),
        'toDate' : datetime(2024,1,1)
})

df.with_columns([pl.col('myDate').is_between('fromDate', 'toDate').alias('inPeriod')])

>>> 
shape: (1, 4)
┌─────────────────────┬─────────────────────┬─────────────────────┬──────────┐
│ myDate              ┆ fromDate            ┆ toDate              ┆ inPeriod │
│ ---                 ┆ ---                 ┆ ---                 ┆ ---      │
│ datetime[μs]        ┆ datetime[μs]        ┆ datetime[μs]        ┆ bool     │
╞═════════════════════╪═════════════════════╪═════════════════════╪══════════╡
│ 2023-01-01 00:00:00 ┆ 2022-01-01 00:00:00 ┆ 2024-01-01 00:00:00 ┆ false    │
└─────────────────────┴─────────────────────┴─────────────────────┴──────────┘

# Note: the correct syntax is
# df.with_columns([pl.col('myDate').is_between(pl.col('fromDate'), pl.col('toDate')).alias('inPeriod')])

Expected behavior

The above should have raised an error.

Either "fromDate" could not be converted to pl.Date()

Or

UTF-8 string can't be compared to pl.Date

Something like that...

Installed versions

---Version info---
Polars: 0.15.15
Index type: UInt32
Platform: Windows-10-10.0.22621-SP0
Python: 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]
---Optional dependencies---
pyarrow: 8.0.0
pandas: 1.5.2
numpy: 1.22.4
fsspec: 2022.8.2
connectorx: 0.3.1
xlsx2csv: <not installed>
matplotlib: 3.6.2```

</details>

mkleinbort-ic avatar Jan 26 '23 14:01 mkleinbort-ic

For a more minimal example:

import polars as pl 
from datetime import datetime

df = pl.DataFrame({
        'someDatetime' : datetime(2023,1,1),
}) 

df < 'Some Text'
>>> 
shape: (1, 1)
┌──────────────┐
│ someDatetime │
│ ---          │
│ bool         │
╞══════════════╡
│ true         │
└──────────────┘

mkleinbort-ic avatar Jan 26 '23 14:01 mkleinbort-ic

Yeap, I agree.

ritchie46 avatar Jan 26 '23 14:01 ritchie46