Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

When to use pandas nat instead of pandas.nat?


Asked by Abdullah Bernard on Dec 09, 2021 FAQ



Since pandas 0.23.4, pandas._libs.tslib.NaTType is completely deprecated, so I have to use pandas.NaT as indicated in 0.23.2 changelog, Well, as the deprecation message suggested, you should use type (pandas.NaT), not NaT directly. So if you imported the name, you can do issubclass (val_type, type (pandas.NaT)).
Accordingly,
Technically, you could also check for Pandas NaT with x != x, following a common pattern used for floating-point NaN. However, this is likely to cause issues with NumPy NaTs, which look very similar and represent the same concept, but are actually a different type with different behavior:
And, In this section, we will discuss missing (also referred to as NA) values in pandas. The choice of using NaN internally to denote missing data was largely for simplicity and performance reasons. Starting from pandas 1.0, some optional data types start experimenting with a native NA scalar using a mask-based approach. See here for more.
Consequently,
For datetime64 [ns] types, NaT represents missing values. This is a pseudo-native sentinel value that can be represented by NumPy in a singular dtype (datetime64 [ns]). pandas objects provide compatibility between NaT and NaN. You can insert missing values by simply assigning to containers.
Next,
Alternatively, the string alias dtype='Int64' (note the capital "I") can be used. See Nullable integer data type for more. For datetime64 [ns] types, NaT represents missing values.