question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected exception on column with NaT

See original GitHub issue

Code Sample

import pandas as pd
import datetime

# 4 examples:

df = pd.DataFrame({0: [1, None]})  # Works
df = pd.DataFrame({0: [None, 1]})  # Works

df = pd.DataFrame({0: [None, datetime.datetime.now()]})  # Exception

# Problem demonstration:
df != df.iloc[0]  # Works with numeric column, fails with NaT

Problem description & expected output.

In the above code, the final test raises an exception with the datetime example, but works with the two numeric examples.

I would expect the NaT case to behave like the numeric example.

Note: a column with datetimes but no NaT makes df != df.iloc[0] work as expected.

Expected Output

I expect the result to be, like for numeric values, a dataframe that answers the question “is the value identical to that in the first row?” (as a dataframe with the same shape).

Output of pd.show_versions()

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: 3.2.2
pip: 9.0.1
setuptools: 36.3.0
Cython: 0.26.1
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.5.0a3
xlrd: 1.1.0
xlwt: 1.2.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
</details>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jorisvandenbosschecommented, Sep 18, 2017

So the fact that seems to make the difference is that here you are comparing to a Series, and not a scalar (the scalar case works ‘fine’, except from the behavioural bug from https://github.com/pandas-dev/pandas/issues/15697 that it should be False instead of True):

In [133]: df
Out[133]: 
                           0
0                        NaT
1 2017-09-18 19:00:27.589018

In [134]: df == pd.NaT
Out[134]: 
      0
0  True
1  True

In [135]: df == df.iloc[0, 0]
Out[135]: 
      0
0  True
1  True

In [136]: df == df.iloc[0]
...
TypeError: Could not operate array([-9223372036854775808]) with block values boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 1

0reactions
eovesoncommented, Nov 19, 2018

Looks like the following test was added in /pandas/tests/arithmetic/test_datetime64.py, as part of GH22163. Would it cover this issue sufficiently already, or should anything else be tested?

def test_dt64_nat_comparison(self):
    # GH#22242, GH#22163 DataFrame considered NaT == ts incorrectly
    ts = pd.Timestamp.now()
    df = pd.DataFrame([ts, pd.NaT])
    expected = pd.DataFrame([True, False])
     result = df == ts
    tm.assert_frame_equal(result, expected)
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - pandas issue with pandas.NaT when changing from ...
In this case, I would do type(df.loc[knownnatdata) and use that as input for the method. For example, I filled a dataframe column with...
Read more >
Plotting a Spatially Enabled DataFrame w/ datetime...
This seems to only be an issue if all the values in a datetime column are NaT. I won't go into details why,...
Read more >
issue with uturn nat, please help! - LIVEcommunity - 25028
Hi All I have a u turn nat rule and security policy in place that has been working fine to allow internal access...
Read more >
Troubleshoot NAT gateways - Amazon Virtual Private Cloud
To view the error message, open the Amazon VPC console, and then choose NAT Gateways. Select the radio button for your NAT gateway,...
Read more >
Looker error catalog | Google Cloud
Query failed with unexpected exception (?), IDE Ex SQL LD D S. A database other than the main connection database is specified in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found