def test_NaT_methods(): # GH 9513 raise_methods = ['astimezone', 'combine', 'ctime', 'dst', 'fromordinal', 'fromtimestamp', 'isocalendar', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'toordinal', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple'] nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today'] nan_methods = ['weekday', 'isoweekday'] for method in raise_methods: if hasattr(NaT, method): with pytest.raises(ValueError): getattr(NaT, method)() for method in nan_methods: if hasattr(NaT, method): assert np.isnan(getattr(NaT, method)()) for method in nat_methods: if hasattr(NaT, method): # see gh-8254 exp_warning = None if method == 'to_datetime': exp_warning = FutureWarning with tm.assert_produces_warning( exp_warning, check_stacklevel=False): assert getattr(NaT, method)() is NaT # GH 12300 assert NaT.isoformat() == 'NaT'
def setup(self, dtype): N = 10000 self.mismatched = [NaT.to_datetime64()] * 2 if dtype in ["boolean", "bool"]: self.series = Series(np.random.randint(0, 2, N)).astype(dtype) self.values = [True, False] elif dtype == "datetime64[ns]": # Note: values here is much larger than non-dt64ns cases # dti has length=115777 dti = date_range(start="2015-10-26", end="2016-01-01", freq="50s") self.series = Series(dti) self.values = self.series._values[::3] self.mismatched = [1, 2] elif dtype in ["category[object]", "category[int]"]: # Note: sizes are different in this case than others np.random.seed(1234) n = 5 * 10**5 sample_size = 100 arr = list(np.random.randint(0, n // 10, size=n)) if dtype == "category[object]": arr = [f"s{i:04d}" for i in arr] self.values = np.random.choice(arr, sample_size) self.series = Series(arr).astype("category") elif dtype in ["str", "string", "arrow_string"]: from pandas.core.arrays.string_arrow import ArrowStringDtype # noqa: F401 try: self.series = Series(tm.makeStringIndex(N), dtype=dtype) except ImportError: raise NotImplementedError self.values = list(self.series[:2]) else: self.series = Series(np.random.randint(1, 10, N)).astype(dtype) self.values = [1, 2] self.cat_values = Categorical(self.values)
def setup(self, dtype): N = 10000 self.mismatched = [NaT.to_datetime64()] * 2 if dtype in ["boolean", "bool"]: self.series = Series(np.random.randint(0, 2, N)).astype(dtype) self.values = [True, False] elif dtype == "datetime64[ns]": # Note: values here is much larger than non-dt64ns cases # dti has length=115777 dti = date_range(start="2015-10-26", end="2016-01-01", freq="50s") self.series = Series(dti) self.values = self.series._values[::3] self.mismatched = [1, 2] elif dtype in ["category[object]", "category[int]"]: # Note: sizes are different in this case than others np.random.seed(1234) n = 5 * 10**5 sample_size = 100 arr = list(np.random.randint(0, n // 10, size=n)) if dtype == "category[object]": arr = [f"s{i:04d}" for i in arr] self.values = np.random.choice(arr, sample_size) self.series = Series(arr).astype("category") else: self.series = Series(np.random.randint(1, 10, N)).astype(dtype) self.values = [1, 2] self.cat_values = Categorical(self.values)
def test_to_numpy_alias(): # GH 24653: alias .to_numpy() for scalars expected = NaT.to_datetime64() result = NaT.to_numpy() assert isna(expected) and isna(result) # GH#44460 result = NaT.to_numpy("M8[s]") assert isinstance(result, np.datetime64) assert result.dtype == "M8[s]" result = NaT.to_numpy("m8[ns]") assert isinstance(result, np.timedelta64) assert result.dtype == "m8[ns]" result = NaT.to_numpy("m8[s]") assert isinstance(result, np.timedelta64) assert result.dtype == "m8[s]" with pytest.raises(ValueError, match="NaT.to_numpy dtype must be a "): NaT.to_numpy(np.int64)
def test_to_numpy_alias(): # GH 24653: alias .to_numpy() for scalars expected = NaT.to_datetime64() result = NaT.to_numpy() assert isna(expected) and isna(result)