Python pandas.NaT() Examples

The following are 30 code examples for showing how to use pandas.NaT(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pandas , or try the search function .

Example 1
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 6 votes vote down vote up
def test_min_max(self):
        arr = TimedeltaArray._from_sequence([
            '3H', '3H', 'NaT', '2H', '5H', '4H',
        ])

        result = arr.min()
        expected = pd.Timedelta('2H')
        assert result == expected

        result = arr.max()
        expected = pd.Timedelta('5H')
        assert result == expected

        result = arr.min(skipna=False)
        assert result is pd.NaT

        result = arr.max(skipna=False)
        assert result is pd.NaT 
Example 2
Project: recruit   Author: Frank-qlu   File: test_period.py    License: Apache License 2.0 6 votes vote down vote up
def test_min_max(self):
        arr = period_array([
            '2000-01-03',
            '2000-01-03',
            'NaT',
            '2000-01-02',
            '2000-01-05',
            '2000-01-04',
        ], freq='D')

        result = arr.min()
        expected = pd.Period('2000-01-02', freq='D')
        assert result == expected

        result = arr.max()
        expected = pd.Period('2000-01-05', freq='D')
        assert result == expected

        result = arr.min(skipna=False)
        assert result is pd.NaT

        result = arr.max(skipna=False)
        assert result is pd.NaT 
Example 3
Project: recruit   Author: Frank-qlu   File: test_datetimes.py    License: Apache License 2.0 6 votes vote down vote up
def test_fillna_preserves_tz(self, method):
        dti = pd.date_range('2000-01-01', periods=5, freq='D', tz='US/Central')
        arr = DatetimeArray(dti, copy=True)
        arr[2] = pd.NaT

        fill_val = dti[1] if method == 'pad' else dti[3]
        expected = DatetimeArray._from_sequence(
            [dti[0], dti[1], fill_val, dti[3], dti[4]],
            freq=None, tz='US/Central'
        )

        result = arr.fillna(method=method)
        tm.assert_extension_array_equal(result, expected)

        # assert that arr and dti were not modified in-place
        assert arr[2] is pd.NaT
        assert dti[2] == pd.Timestamp('2000-01-03', tz='US/Central') 
Example 4
Project: recruit   Author: Frank-qlu   File: test_datetimes.py    License: Apache License 2.0 6 votes vote down vote up
def test_min_max(self, tz):
        arr = DatetimeArray._from_sequence([
            '2000-01-03',
            '2000-01-03',
            'NaT',
            '2000-01-02',
            '2000-01-05',
            '2000-01-04',
        ], tz=tz)

        result = arr.min()
        expected = pd.Timestamp('2000-01-02', tz=tz)
        assert result == expected

        result = arr.max()
        expected = pd.Timestamp('2000-01-05', tz=tz)
        assert result == expected

        result = arr.min(skipna=False)
        assert result is pd.NaT

        result = arr.max(skipna=False)
        assert result is pd.NaT 
Example 5
Project: recruit   Author: Frank-qlu   File: test_datetimelike.py    License: Apache License 2.0 6 votes vote down vote up
def test_searchsorted(self):
        data = np.arange(10, dtype='i8') * 24 * 3600 * 10**9
        arr = self.array_cls(data, freq='D')

        # scalar
        result = arr.searchsorted(arr[1])
        assert result == 1

        result = arr.searchsorted(arr[2], side="right")
        assert result == 3

        # own-type
        result = arr.searchsorted(arr[1:3])
        expected = np.array([1, 2], dtype=np.intp)
        tm.assert_numpy_array_equal(result, expected)

        result = arr.searchsorted(arr[1:3], side="right")
        expected = np.array([2, 3], dtype=np.intp)
        tm.assert_numpy_array_equal(result, expected)

        # Following numpy convention, NaT goes at the beginning
        #  (unlike NaN which goes at the end)
        result = arr.searchsorted(pd.NaT)
        assert result == 0 
Example 6
Project: recruit   Author: Frank-qlu   File: test_construction.py    License: Apache License 2.0 6 votes vote down vote up
def test_float64_ns_rounded(self):
        # GH#23539 without specifying a unit, floats are regarded as nanos,
        #  and fractional portions are truncated
        tdi = TimedeltaIndex([2.3, 9.7])
        expected = TimedeltaIndex([2, 9])
        tm.assert_index_equal(tdi, expected)

        # integral floats are non-lossy
        tdi = TimedeltaIndex([2.0, 9.0])
        expected = TimedeltaIndex([2, 9])
        tm.assert_index_equal(tdi, expected)

        # NaNs get converted to NaT
        tdi = TimedeltaIndex([2.0, np.nan])
        expected = TimedeltaIndex([pd.Timedelta(nanoseconds=2), pd.NaT])
        tm.assert_index_equal(tdi, expected) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_tools.py    License: Apache License 2.0 6 votes vote down vote up
def test_to_timedelta_on_missing_values(self):
        # GH5438
        timedelta_NaT = np.timedelta64('NaT')

        actual = pd.to_timedelta(Series(['00:00:01', np.nan]))
        expected = Series([np.timedelta64(1000000000, 'ns'),
                           timedelta_NaT], dtype='<m8[ns]')
        assert_series_equal(actual, expected)

        actual = pd.to_timedelta(Series(['00:00:01', pd.NaT]))
        assert_series_equal(actual, expected)

        actual = pd.to_timedelta(np.nan)
        assert actual.value == timedelta_NaT.astype('int64')

        actual = pd.to_timedelta(pd.NaT)
        assert actual.value == timedelta_NaT.astype('int64') 
Example 8
Project: recruit   Author: Frank-qlu   File: test_ops.py    License: Apache License 2.0 6 votes vote down vote up
def test_nat(self):
        assert pd.TimedeltaIndex._na_value is pd.NaT
        assert pd.TimedeltaIndex([])._na_value is pd.NaT

        idx = pd.TimedeltaIndex(['1 days', '2 days'])
        assert idx._can_hold_na

        tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
        assert idx.hasnans is False
        tm.assert_numpy_array_equal(idx._nan_idxs,
                                    np.array([], dtype=np.intp))

        idx = pd.TimedeltaIndex(['1 days', 'NaT'])
        assert idx._can_hold_na

        tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
        assert idx.hasnans is True
        tm.assert_numpy_array_equal(idx._nan_idxs,
                                    np.array([1], dtype=np.intp)) 
Example 9
Project: recruit   Author: Frank-qlu   File: datetimelike.py    License: Apache License 2.0 6 votes vote down vote up
def test_map_dictlike(self, mapper):
        expected = self.index + self.index.freq

        # don't compare the freqs
        if isinstance(expected, pd.DatetimeIndex):
            expected.freq = None

        result = self.index.map(mapper(expected, self.index))
        tm.assert_index_equal(result, expected)

        expected = pd.Index([pd.NaT] + self.index[1:].tolist())
        result = self.index.map(mapper(expected, self.index))
        tm.assert_index_equal(result, expected)

        # empty map; these map to np.nan because we cannot know
        # to re-infer things
        expected = pd.Index([np.nan] * len(self.index))
        result = self.index.map(mapper([], []))
        tm.assert_index_equal(result, expected) 
Example 10
Project: recruit   Author: Frank-qlu   File: test_numeric.py    License: Apache License 2.0 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example 11
Project: recruit   Author: Frank-qlu   File: test_indexing.py    License: Apache License 2.0 6 votes vote down vote up
def test_where_other(self):
        # other is ndarray or Index
        i = pd.date_range('20130101', periods=3, tz='US/Eastern')

        for arr in [np.nan, pd.NaT]:
            result = i.where(notna(i), other=np.nan)
            expected = i
            tm.assert_index_equal(result, expected)

        i2 = i.copy()
        i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
        result = i.where(notna(i2), i2)
        tm.assert_index_equal(result, i2)

        i2 = i.copy()
        i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
        result = i.where(notna(i2), i2.values)
        tm.assert_index_equal(result, i2) 
Example 12
Project: recruit   Author: Frank-qlu   File: test_construction.py    License: Apache License 2.0 6 votes vote down vote up
def test_categorical_preserves_tz(self):
        # GH#18664 retain tz when going DTI-->Categorical-->DTI
        # TODO: parametrize over DatetimeIndex/DatetimeArray
        #  once CategoricalIndex(DTA) works

        dti = pd.DatetimeIndex(
            [pd.NaT, '2015-01-01', '1999-04-06 15:14:13', '2015-01-01'],
            tz='US/Eastern')

        ci = pd.CategoricalIndex(dti)
        carr = pd.Categorical(dti)
        cser = pd.Series(ci)

        for obj in [ci, carr, cser]:
            result = pd.DatetimeIndex(obj)
            tm.assert_index_equal(result, dti) 
Example 13
Project: recruit   Author: Frank-qlu   File: test_timezones.py    License: Apache License 2.0 6 votes vote down vote up
def test_dti_tz_localize_nonexistent_raise_coerce(self):
        # GH#13057
        times = ['2015-03-08 01:00', '2015-03-08 02:00', '2015-03-08 03:00']
        index = DatetimeIndex(times)
        tz = 'US/Eastern'
        with pytest.raises(pytz.NonExistentTimeError):
            index.tz_localize(tz=tz)

        with pytest.raises(pytz.NonExistentTimeError):
            with tm.assert_produces_warning(FutureWarning):
                index.tz_localize(tz=tz, errors='raise')

        with tm.assert_produces_warning(FutureWarning,
                                        clear=FutureWarning,
                                        check_stacklevel=False):
            result = index.tz_localize(tz=tz, errors='coerce')
        test_times = ['2015-03-08 01:00-05:00', 'NaT',
                      '2015-03-08 03:00-04:00']
        dti = to_datetime(test_times, utc=True)
        expected = dti.tz_convert('US/Eastern')
        tm.assert_index_equal(result, expected) 
Example 14
Project: recruit   Author: Frank-qlu   File: test_tools.py    License: Apache License 2.0 6 votes vote down vote up
def test_datetime_outofbounds_scalar(self, value, format, infer):
        # GH24763
        res = pd.to_datetime(value, errors='ignore', format=format,
                             infer_datetime_format=infer)
        assert res == value

        res = pd.to_datetime(value, errors='coerce', format=format,
                             infer_datetime_format=infer)
        assert res is pd.NaT

        if format is not None:
            with pytest.raises(ValueError):
                pd.to_datetime(value, errors='raise', format=format,
                               infer_datetime_format=infer)
        else:
            with pytest.raises(OutOfBoundsDatetime):
                pd.to_datetime(value, errors='raise', format=format,
                               infer_datetime_format=infer) 
Example 15
Project: recruit   Author: Frank-qlu   File: test_tools.py    License: Apache License 2.0 6 votes vote down vote up
def test_iso_8601_strings_with_different_offsets(self):
        # GH 17697, 11736
        ts_strings = ["2015-11-18 15:30:00+05:30",
                      "2015-11-18 16:30:00+06:30",
                      NaT]
        result = to_datetime(ts_strings)
        expected = np.array([datetime(2015, 11, 18, 15, 30,
                                      tzinfo=tzoffset(None, 19800)),
                             datetime(2015, 11, 18, 16, 30,
                                      tzinfo=tzoffset(None, 23400)),
                             NaT],
                            dtype=object)
        # GH 21864
        expected = Index(expected)
        tm.assert_index_equal(result, expected)

        result = to_datetime(ts_strings, utc=True)
        expected = DatetimeIndex([Timestamp(2015, 11, 18, 10),
                                  Timestamp(2015, 11, 18, 10),
                                  NaT], tz='UTC')
        tm.assert_index_equal(result, expected) 
Example 16
Project: recruit   Author: Frank-qlu   File: test_tools.py    License: Apache License 2.0 6 votes vote down vote up
def test_unit_mixed(self, cache):

        # mixed integers/datetimes
        expected = DatetimeIndex(['2013-01-01', 'NaT', 'NaT'])
        arr = [pd.Timestamp('20130101'), 1.434692e+18, 1.432766e+18]
        result = pd.to_datetime(arr, errors='coerce', cache=cache)
        tm.assert_index_equal(result, expected)

        with pytest.raises(ValueError):
            pd.to_datetime(arr, errors='raise', cache=cache)

        expected = DatetimeIndex(['NaT',
                                  'NaT',
                                  '2013-01-01'])
        arr = [1.434692e+18, 1.432766e+18, pd.Timestamp('20130101')]
        result = pd.to_datetime(arr, errors='coerce', cache=cache)
        tm.assert_index_equal(result, expected)

        with pytest.raises(ValueError):
            pd.to_datetime(arr, errors='raise', cache=cache) 
Example 17
Project: recruit   Author: Frank-qlu   File: test_astype.py    License: Apache License 2.0 6 votes vote down vote up
def test_astype_datetime64(self):
        # GH 13149, GH 13209
        idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])

        result = idx.astype('datetime64[ns]')
        tm.assert_index_equal(result, idx)
        assert result is not idx

        result = idx.astype('datetime64[ns]', copy=False)
        tm.assert_index_equal(result, idx)
        assert result is idx

        idx_tz = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN], tz='EST')
        result = idx_tz.astype('datetime64[ns]')
        expected = DatetimeIndex(['2016-05-16 05:00:00', 'NaT', 'NaT', 'NaT'],
                                 dtype='datetime64[ns]')
        tm.assert_index_equal(result, expected) 
Example 18
Project: recruit   Author: Frank-qlu   File: test_ops.py    License: Apache License 2.0 6 votes vote down vote up
def test_nat(self, tz_naive_fixture):
        tz = tz_naive_fixture
        assert pd.DatetimeIndex._na_value is pd.NaT
        assert pd.DatetimeIndex([])._na_value is pd.NaT

        idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], tz=tz)
        assert idx._can_hold_na

        tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
        assert idx.hasnans is False
        tm.assert_numpy_array_equal(idx._nan_idxs,
                                    np.array([], dtype=np.intp))

        idx = pd.DatetimeIndex(['2011-01-01', 'NaT'], tz=tz)
        assert idx._can_hold_na

        tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
        assert idx.hasnans is True
        tm.assert_numpy_array_equal(idx._nan_idxs,
                                    np.array([1], dtype=np.intp)) 
Example 19
Project: recruit   Author: Frank-qlu   File: test_datetime.py    License: Apache License 2.0 5 votes vote down vote up
def data_missing(dtype):
    return DatetimeArray(
        np.array(['NaT', '2000-01-01'], dtype='datetime64[ns]'),
        dtype=dtype
    ) 
Example 20
Project: recruit   Author: Frank-qlu   File: test_datetime.py    License: Apache License 2.0 5 votes vote down vote up
def data_missing_for_sorting(dtype):
    a = pd.Timestamp('2000-01-01')
    b = pd.Timestamp('2000-01-02')
    return DatetimeArray(np.array([b, 'NaT', a], dtype='datetime64[ns]'),
                         dtype=dtype) 
Example 21
Project: recruit   Author: Frank-qlu   File: test_datetime.py    License: Apache License 2.0 5 votes vote down vote up
def data_for_grouping(dtype):
    """
        Expected to be like [B, B, NA, NA, A, A, B, C]

        Where A < B < C and NA is missing
    """
    a = pd.Timestamp('2000-01-01')
    b = pd.Timestamp('2000-01-02')
    c = pd.Timestamp('2000-01-03')
    na = 'NaT'
    return DatetimeArray(np.array([b, b, na, na, a, a, b, c],
                                  dtype='datetime64[ns]'),
                         dtype=dtype) 
Example 22
Project: recruit   Author: Frank-qlu   File: test_datetime.py    License: Apache License 2.0 5 votes vote down vote up
def na_cmp():
    def cmp(a, b):
        return a is pd.NaT and a is b
    return cmp 
Example 23
Project: recruit   Author: Frank-qlu   File: test_datetime.py    License: Apache License 2.0 5 votes vote down vote up
def na_value():
    return pd.NaT


# ---------------------------------------------------------------------------- 
Example 24
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 5 votes vote down vote up
def test_abs(self):
        vals = np.array([-3600 * 10**9, 'NaT', 7200 * 10**9], dtype='m8[ns]')
        arr = TimedeltaArray(vals)

        evals = np.array([3600 * 10**9, 'NaT', 7200 * 10**9], dtype='m8[ns]')
        expected = TimedeltaArray(evals)

        result = abs(arr)
        tm.assert_timedelta_array_equal(result, expected) 
Example 25
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 5 votes vote down vote up
def test_neg(self):
        vals = np.array([-3600 * 10**9, 'NaT', 7200 * 10**9], dtype='m8[ns]')
        arr = TimedeltaArray(vals)

        evals = np.array([3600 * 10**9, 'NaT', -7200 * 10**9], dtype='m8[ns]')
        expected = TimedeltaArray(evals)

        result = -arr
        tm.assert_timedelta_array_equal(result, expected) 
Example 26
Project: recruit   Author: Frank-qlu   File: test_timedeltas.py    License: Apache License 2.0 5 votes vote down vote up
def test_min_max_empty(self, skipna):
        arr = TimedeltaArray._from_sequence([])
        result = arr.min(skipna=skipna)
        assert result is pd.NaT

        result = arr.max(skipna=skipna)
        assert result is pd.NaT 
Example 27
Project: recruit   Author: Frank-qlu   File: test_datetimes.py    License: Apache License 2.0 5 votes vote down vote up
def test_value_counts_preserves_tz(self):
        dti = pd.date_range('2000', periods=2, freq='D', tz='US/Central')
        arr = DatetimeArray(dti).repeat([4, 3])

        result = arr.value_counts()

        # Note: not tm.assert_index_equal, since `freq`s do not match
        assert result.index.equals(dti)

        arr[-2] = pd.NaT
        result = arr.value_counts()
        expected = pd.Series([1, 4, 2],
                             index=[pd.NaT, dti[0], dti[1]])
        tm.assert_series_equal(result, expected) 
Example 28
Project: recruit   Author: Frank-qlu   File: test_datetimes.py    License: Apache License 2.0 5 votes vote down vote up
def test_min_max_empty(self, skipna, tz):
        arr = DatetimeArray._from_sequence([], tz=tz)
        result = arr.min(skipna=skipna)
        assert result is pd.NaT

        result = arr.max(skipna=skipna)
        assert result is pd.NaT 
Example 29
Project: recruit   Author: Frank-qlu   File: test_datetimelike.py    License: Apache License 2.0 5 votes vote down vote up
def timedelta_index(request):
    """
    A fixture to provide TimedeltaIndex objects with different frequencies.
     Most TimedeltaArray behavior is already tested in TimedeltaIndex tests,
    so here we just test that the TimedeltaArray behavior matches
    the TimedeltaIndex behavior.
    """
    # TODO: flesh this out
    return pd.TimedeltaIndex(['1 Day', '3 Hours', 'NaT']) 
Example 30
Project: recruit   Author: Frank-qlu   File: test_datetimelike.py    License: Apache License 2.0 5 votes vote down vote up
def test_concat_same_type(self):
        data = np.arange(10, dtype='i8') * 24 * 3600 * 10**9

        idx = self.index_cls._simple_new(data, freq='D').insert(0, pd.NaT)
        arr = self.array_cls(idx)

        result = arr._concat_same_type([arr[:-1], arr[1:], arr])
        expected = idx._concat_same_dtype([idx[:-1], idx[1:], idx], None)

        tm.assert_index_equal(self.index_cls(result), expected)