BUG: Raise when casting NaT to int by dsaxton · Pull Request #28492 · pandas-dev/pandas (original) (raw)

This hits both Series.astype, Index.astype, and DataFrame.astype

In [6]: a = [pd.Timestamp('2000'), pd.NaT]

In [7]: pd.Series(a).astype(int) Out[7]: 0 946684800000000000 1 -9223372036854775808 dtype: int64

In [8]: pd.Index(a).astype(int) Out[8]: Int64Index([946684800000000000, -9223372036854775808], dtype='int64')

In [10]: pd.DataFrame({"a": a}).astype(int) Out[10]: a 0 946684800000000000 1 -9223372036854775808

In addition to the astype_nansafe unit tests, we'd ideally also test these user facing ones.

And we should double check: we want this behavior right? Some people may be relying on this behavior (treating pd.NaT as iNaT). This feels like a bigger change than https://github.com/pandas-dev/pandas/pull/28438/files. I might prefer a warning first that this will change (though how would people work around it?).