def visualise_df_datetime_columns(df, datetime_columns=None): if datetime_columns is None: datetime_columns = pdutils.get_df_datetime_columns(df) for c in datetime_columns: visualise_date_iterable( [conv.to_python_datetime(x).date() for x in df[c].values], name='%s.date' % c) visualise_time_iterable( [conv.to_python_datetime(x).time() for x in df[c].values], name='%s.time' % c)
def test_to_python_datetime(self): self.assertEqual( conv.to_python_datetime(np.datetime64('2017-11-19T10:49:31.357')), dt.datetime(2017, 11, 19, 10, 49, 31, 357000)) self.assertEqual( conv.to_python_datetime( pd.Timestamp(dt.datetime(2017, 11, 19, 10, 49, 31, 357000))), dt.datetime(2017, 11, 19, 10, 49, 31, 357000)) self.assertEqual(conv.to_python_datetime('2017.11.19T10:49:31.357'), dt.datetime(2017, 11, 19, 10, 49, 31, 357000))
def plus_timedelta(x, timedelta, on_overflow='raise', raise_value_error=True): if isinstance(x, dt.time): return time_plus_timedelta(x, timedelta, on_overflow) elif checks.is_some_datetime(x): return conv.to_python_datetime(x, allow_dates=True) + timedelta else: if raise_value_error: raise ValueError('Invalid timedelta: "%s"' % str(timedelta)) return None
def _temporal_comparison(x, y, comp, return_pandas_series=True): import pandas as pd if checks.is_iterable_not_string(x): if checks.is_iterable_not_string(y): result = [ _temporal_comparison(ex, ey, comp) for ex, ey in zip(x, y) ] return pd.Series(result) if return_pandas_series else result else: result = [_temporal_comparison(ex, y, comp) for ex in x] return pd.Series(result) if return_pandas_series else result elif checks.is_iterable_not_string(y): result = [_temporal_comparison(x, ey, comp) for ey in y] return pd.Series(result) if return_pandas_series else result elif checks.is_some_time(x) or checks.is_some_time(y): return comp(conv.to_python_time(x), conv.to_python_time(y)) elif checks.is_some_date(x) or checks.is_some_date(y): return comp(conv.to_python_date(x), conv.to_python_date(y)) else: return comp(conv.to_python_datetime(x), conv.to_python_datetime(y))
def test_to_python_datetime(self): pandas_timestamp = pd.Timestamp( dt.datetime(2019, 9, 10, 14, 19, 31, 357000)) python_datetime = dt.datetime(2019, 9, 10, 14, 19, 31, 357000) converted = conv.to_python_datetime(pandas_timestamp) self.assertEqual(converted, python_datetime) pandas_timestamps = [ pd.Timestamp(dt.datetime(2019, 9, 10, 13, 19, 31, 357000)), pd.Timestamp(dt.datetime(2019, 9, 10, 14, 19, 31, 357000)) ] python_datetimes = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), dt.datetime(2019, 9, 10, 14, 19, 31, 357000) ] converted = conv.to_python_datetime(pandas_timestamps) self.assertEqual(converted, python_datetimes) numpy_datetime64 = np.datetime64('2019-09-10T14:19:31.357') python_datetime = dt.datetime(2019, 9, 10, 14, 19, 31, 357000) converted = conv.to_python_datetime(numpy_datetime64) self.assertEqual(converted, python_datetime) numpy_datetime64s = [ np.datetime64('2019-09-10T13:19:31.357'), np.datetime64('2019-09-10T14:19:31.357') ] python_datetimes = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), dt.datetime(2019, 9, 10, 14, 19, 31, 357000) ] converted = conv.to_python_datetime(numpy_datetime64s) self.assertEqual(converted, python_datetimes) python_datetime = dt.datetime(2019, 9, 10, 14, 19, 31, 357000) converted = conv.to_python_datetime(python_datetime) self.assertEqual(converted, python_datetime) python_datetimes = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), dt.datetime(2019, 9, 10, 14, 19, 31, 357000) ] converted = conv.to_python_datetime(python_datetimes) self.assertEqual(converted, python_datetimes) datetime_str = '2019.09.10T14:19:31.357' python_datetime = dt.datetime(2019, 9, 10, 14, 19, 31, 357000) converted = conv.to_python_datetime(datetime_str) self.assertEqual(converted, python_datetime) datetime_strs = ['2019.09.10T13:19:31.357', '2019.09.10T14:19:31.357'] python_datetimes = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), dt.datetime(2019, 9, 10, 14, 19, 31, 357000) ] converted = conv.to_python_datetime(datetime_strs) self.assertEqual(converted, python_datetimes) misc = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), '2019.09.10T14:19:31.357' ] python_datetimes = [ dt.datetime(2019, 9, 10, 13, 19, 31, 357000), dt.datetime(2019, 9, 10, 14, 19, 31, 357000) ] converted = conv.to_python_datetime(misc) self.assertEqual(converted, python_datetimes)