Python pandas.core.tools.datetimes.to_datetime() Examples
The following are 30
code examples of pandas.core.tools.datetimes.to_datetime().
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 also want to check out all available functions/classes of the module
pandas.core.tools.datetimes
, or try the search function
.
Example #1
Source File: parse_dates.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_parse_tz_aware(self): # See gh-1693 import pytz data = StringIO("Date,x\n2012-06-13T01:39:00Z,0.5") # it works result = self.read_csv(data, index_col=0, parse_dates=True) stamp = result.index[0] assert stamp.minute == 39 try: assert result.index.tz is pytz.utc except AssertionError: # hello Yaroslav arr = result.index.to_pydatetime() result = tools.to_datetime(arr, utc=True)[0] assert stamp.minute == result.minute assert stamp.hour == result.hour assert stamp.day == result.day
Example #2
Source File: sql.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def _handle_date_column(col, utc=None, format=None): if isinstance(format, dict): return to_datetime(col, errors='ignore', **format) else: # Allow passing of formatting string for integers # GH17855 if format is None and (issubclass(col.dtype.type, np.floating) or issubclass(col.dtype.type, np.integer)): format = 's' if format in ['D', 'd', 'h', 'm', 's', 'ms', 'us', 'ns']: return to_datetime(col, errors='coerce', unit=format, utc=utc) elif is_datetime64tz_dtype(col): # coerce to UTC timezone # GH11216 return to_datetime(col, utc=True) else: return to_datetime(col, errors='coerce', format=format, utc=utc)
Example #3
Source File: sql.py From vnpy_crypto with MIT License | 6 votes |
def _handle_date_column(col, utc=None, format=None): if isinstance(format, dict): return to_datetime(col, errors='ignore', **format) else: # Allow passing of formatting string for integers # GH17855 if format is None and (issubclass(col.dtype.type, np.floating) or issubclass(col.dtype.type, np.integer)): format = 's' if format in ['D', 'd', 'h', 'm', 's', 'ms', 'us', 'ns']: return to_datetime(col, errors='coerce', unit=format, utc=utc) elif is_datetime64tz_dtype(col): # coerce to UTC timezone # GH11216 return (to_datetime(col, errors='coerce') .astype('datetime64[ns, UTC]')) else: return to_datetime(col, errors='coerce', format=format, utc=utc)
Example #4
Source File: parse_dates.py From vnpy_crypto with MIT License | 6 votes |
def test_parse_tz_aware(self): # See gh-1693 import pytz data = StringIO("Date,x\n2012-06-13T01:39:00Z,0.5") # it works result = self.read_csv(data, index_col=0, parse_dates=True) stamp = result.index[0] assert stamp.minute == 39 try: assert result.index.tz is pytz.utc except AssertionError: # hello Yaroslav arr = result.index.to_pydatetime() result = tools.to_datetime(arr, utc=True)[0] assert stamp.minute == result.minute assert stamp.hour == result.hour assert stamp.day == result.day
Example #5
Source File: sql.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def _handle_date_column(col, utc=None, format=None): if isinstance(format, dict): return to_datetime(col, errors='ignore', **format) else: if format in ['D', 's', 'ms', 'us', 'ns']: return to_datetime(col, errors='coerce', unit=format, utc=utc) elif (issubclass(col.dtype.type, np.floating) or issubclass(col.dtype.type, np.integer)): # parse dates as timestamp format = 's' if format is None else format return to_datetime(col, errors='coerce', unit=format, utc=utc) elif is_datetime64tz_dtype(col): # coerce to UTC timezone # GH11216 return (to_datetime(col, errors='coerce') .astype('datetime64[ns, UTC]')) else: return to_datetime(col, errors='coerce', format=format, utc=utc)
Example #6
Source File: parse_dates.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_parse_tz_aware(self): # See gh-1693 import pytz data = StringIO("Date,x\n2012-06-13T01:39:00Z,0.5") # it works result = self.read_csv(data, index_col=0, parse_dates=True) stamp = result.index[0] assert stamp.minute == 39 try: assert result.index.tz is pytz.utc except AssertionError: # hello Yaroslav arr = result.index.to_pydatetime() result = tools.to_datetime(arr, utc=True)[0] assert stamp.minute == result.minute assert stamp.hour == result.hour assert stamp.day == result.day
Example #7
Source File: sql.py From recruit with Apache License 2.0 | 6 votes |
def _handle_date_column(col, utc=None, format=None): if isinstance(format, dict): return to_datetime(col, errors='ignore', **format) else: # Allow passing of formatting string for integers # GH17855 if format is None and (issubclass(col.dtype.type, np.floating) or issubclass(col.dtype.type, np.integer)): format = 's' if format in ['D', 'd', 'h', 'm', 's', 'ms', 'us', 'ns']: return to_datetime(col, errors='coerce', unit=format, utc=utc) elif is_datetime64tz_dtype(col): # coerce to UTC timezone # GH11216 return to_datetime(col, utc=True) else: return to_datetime(col, errors='coerce', format=format, utc=utc)
Example #8
Source File: test_frequencies.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_non_datetimeindex(self): dates = to_datetime(['1/1/2000', '1/2/2000', '1/3/2000']) assert frequencies.infer_freq(dates) == 'D'
Example #9
Source File: parsers.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _make_date_converter(date_parser=None, dayfirst=False, infer_datetime_format=False): def converter(*date_cols): if date_parser is None: strs = _concat_date_cols(date_cols) try: return tools.to_datetime( ensure_object(strs), utc=None, box=False, dayfirst=dayfirst, errors='ignore', infer_datetime_format=infer_datetime_format ) except ValueError: return tools.to_datetime( parsing.try_parse_dates(strs, dayfirst=dayfirst)) else: try: result = tools.to_datetime( date_parser(*date_cols), errors='ignore') if isinstance(result, datetime.datetime): raise Exception('scalar parser') return result except Exception: try: return tools.to_datetime( parsing.try_parse_dates(_concat_date_cols(date_cols), parser=date_parser, dayfirst=dayfirst), errors='ignore') except Exception: return generic_parser(date_parser, *date_cols) return converter
Example #10
Source File: _converter.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def time2num(d): if isinstance(d, compat.string_types): parsed = tools.to_datetime(d) if not isinstance(parsed, datetime): raise ValueError('Could not parse time %s' % d) return _to_ordinalf(parsed.time()) if isinstance(d, pydt.time): return _to_ordinalf(d) return d
Example #11
Source File: _converter.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _convert_1d(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, np.datetime64): return _dt_to_float_ordinal(lib.Timestamp(values)) elif isinstance(values, pydt.time): return dates.date2num(values) elif (is_integer(values) or is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray, Index)): if isinstance(values, Index): values = values.values if not isinstance(values, np.ndarray): values = com._asarray_tuplesafe(values) if is_integer_dtype(values) or is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: values = _dt_to_float_ordinal(values) return values
Example #12
Source File: datetimes.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def to_datetime(self, dayfirst=False): return self.copy()
Example #13
Source File: datetimes.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _generate_regular_range(start, end, periods, offset): if isinstance(offset, Tick): stride = offset.nanos if periods is None: b = Timestamp(start).value # cannot just use e = Timestamp(end) + 1 because arange breaks when # stride is too large, see GH10887 e = (b + (Timestamp(end).value - b) // stride * stride + stride // 2 + 1) # end.tz == start.tz by this point due to _generate implementation tz = start.tz elif start is not None: b = Timestamp(start).value e = b + np.int64(periods) * stride tz = start.tz elif end is not None: e = Timestamp(end).value + stride b = e - np.int64(periods) * stride tz = end.tz else: raise ValueError("at least 'start' or 'end' should be specified " "if a 'period' is given.") data = np.arange(b, e, stride, dtype=np.int64) data = DatetimeIndex._simple_new(data, None, tz=tz) else: if isinstance(start, Timestamp): start = start.to_pydatetime() if isinstance(end, Timestamp): end = end.to_pydatetime() xdr = generate_range(start=start, end=end, periods=periods, offset=offset) dates = list(xdr) # utc = len(dates) > 0 and dates[0].tzinfo is not None data = tools.to_datetime(dates) return data
Example #14
Source File: parsers.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _make_date_converter(date_parser=None, dayfirst=False, infer_datetime_format=False): def converter(*date_cols): if date_parser is None: strs = _concat_date_cols(date_cols) try: return tools.to_datetime( _ensure_object(strs), utc=None, box=False, dayfirst=dayfirst, errors='ignore', infer_datetime_format=infer_datetime_format ) except: return tools.to_datetime( parsing.try_parse_dates(strs, dayfirst=dayfirst)) else: try: result = tools.to_datetime( date_parser(*date_cols), errors='ignore') if isinstance(result, datetime.datetime): raise Exception('scalar parser') return result except Exception: try: return tools.to_datetime( parsing.try_parse_dates(_concat_date_cols(date_cols), parser=date_parser, dayfirst=dayfirst), errors='ignore') except Exception: return generic_parser(date_parser, *date_cols) return converter
Example #15
Source File: _converter.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def time2num(d): if isinstance(d, compat.string_types): parsed = tools.to_datetime(d) if not isinstance(parsed, datetime): raise ValueError('Could not parse time %s' % d) return _to_ordinalf(parsed.time()) if isinstance(d, pydt.time): return _to_ordinalf(d) return d
Example #16
Source File: _converter.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _convert_1d(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, np.datetime64): return _dt_to_float_ordinal(lib.Timestamp(values)) elif isinstance(values, pydt.time): return dates.date2num(values) elif (is_integer(values) or is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray, Index)): if isinstance(values, Index): values = values.values if not isinstance(values, np.ndarray): values = com._asarray_tuplesafe(values) if is_integer_dtype(values) or is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: values = _dt_to_float_ordinal(values) return values
Example #17
Source File: test_combine_concat.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_combine_first_dt64(self): from pandas.core.tools.datetimes import to_datetime s0 = to_datetime(Series(["2010", np.NaN])) s1 = to_datetime(Series([np.NaN, "2011"])) rs = s0.combine_first(s1) xp = to_datetime(Series(['2010', '2011'])) assert_series_equal(rs, xp) s0 = to_datetime(Series(["2010", np.NaN])) s1 = Series([np.NaN, "2011"]) rs = s0.combine_first(s1) xp = Series([datetime(2010, 1, 1), '2011']) assert_series_equal(rs, xp)
Example #18
Source File: test_combine_concat.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_combine_first_dt64(self): from pandas.core.tools.datetimes import to_datetime s0 = to_datetime(Series(["2010", np.NaN])) s1 = to_datetime(Series([np.NaN, "2011"])) rs = s0.combine_first(s1) xp = to_datetime(Series(['2010', '2011'])) assert_series_equal(rs, xp) s0 = to_datetime(Series(["2010", np.NaN])) s1 = Series([np.NaN, "2011"]) rs = s0.combine_first(s1) xp = Series([datetime(2010, 1, 1), '2011']) assert_series_equal(rs, xp)
Example #19
Source File: test_frequencies.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_non_datetimeindex(self): dates = to_datetime(['1/1/2000', '1/2/2000', '1/3/2000']) assert frequencies.infer_freq(dates) == 'D'
Example #20
Source File: _converter.py From recruit with Apache License 2.0 | 5 votes |
def _convert_1d(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, np.datetime64): return _dt_to_float_ordinal(tslibs.Timestamp(values)) elif isinstance(values, pydt.time): return dates.date2num(values) elif (is_integer(values) or is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray, Index, ABCSeries)): if isinstance(values, ABCSeries): # https://github.com/matplotlib/matplotlib/issues/11391 # Series was skipped. Convert to DatetimeIndex to get asi8 values = Index(values) if isinstance(values, Index): values = values.values if not isinstance(values, np.ndarray): values = com.asarray_tuplesafe(values) if is_integer_dtype(values) or is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: values = _dt_to_float_ordinal(values) return values
Example #21
Source File: datetimes.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _generate_regular_range(start, end, periods, offset): if isinstance(offset, Tick): stride = offset.nanos if periods is None: b = Timestamp(start).value # cannot just use e = Timestamp(end) + 1 because arange breaks when # stride is too large, see GH10887 e = (b + (Timestamp(end).value - b) // stride * stride + stride // 2 + 1) # end.tz == start.tz by this point due to _generate implementation tz = start.tz elif start is not None: b = Timestamp(start).value e = b + np.int64(periods) * stride tz = start.tz elif end is not None: e = Timestamp(end).value + stride b = e - np.int64(periods) * stride tz = end.tz else: raise ValueError("at least 'start' or 'end' should be specified " "if a 'period' is given.") data = np.arange(b, e, stride, dtype=np.int64) data = DatetimeIndex._simple_new(data, None, tz=tz) else: if isinstance(start, Timestamp): start = start.to_pydatetime() if isinstance(end, Timestamp): end = end.to_pydatetime() xdr = generate_range(start=start, end=end, periods=periods, offset=offset) dates = list(xdr) # utc = len(dates) > 0 and dates[0].tzinfo is not None data = tools.to_datetime(dates) return data
Example #22
Source File: _converter.py From twitter-stock-recommendation with MIT License | 5 votes |
def _convert_1d(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, np.datetime64): return _dt_to_float_ordinal(tslib.Timestamp(values)) elif isinstance(values, pydt.time): return dates.date2num(values) elif (is_integer(values) or is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray, Index)): if isinstance(values, Index): values = values.values if not isinstance(values, np.ndarray): values = com._asarray_tuplesafe(values) if is_integer_dtype(values) or is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: values = _dt_to_float_ordinal(values) return values
Example #23
Source File: parsers.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _make_date_converter(date_parser=None, dayfirst=False, infer_datetime_format=False): def converter(*date_cols): if date_parser is None: strs = _concat_date_cols(date_cols) try: return tools.to_datetime( _ensure_object(strs), utc=None, box=False, dayfirst=dayfirst, errors='ignore', infer_datetime_format=infer_datetime_format ) except: return tools.to_datetime( parsing.try_parse_dates(strs, dayfirst=dayfirst)) else: try: result = tools.to_datetime( date_parser(*date_cols), errors='ignore') if isinstance(result, datetime.datetime): raise Exception('scalar parser') return result except Exception: try: return tools.to_datetime( parsing.try_parse_dates(_concat_date_cols(date_cols), parser=date_parser, dayfirst=dayfirst), errors='ignore') except Exception: return generic_parser(date_parser, *date_cols) return converter
Example #24
Source File: _converter.py From coffeegrindsize with MIT License | 5 votes |
def time2num(d): if isinstance(d, compat.string_types): parsed = tools.to_datetime(d) if not isinstance(parsed, datetime): raise ValueError('Could not parse time {d}'.format(d=d)) return _to_ordinalf(parsed.time()) if isinstance(d, pydt.time): return _to_ordinalf(d) return d
Example #25
Source File: _converter.py From coffeegrindsize with MIT License | 5 votes |
def _convert_1d(values, unit, axis): def try_parse(values): try: return _dt_to_float_ordinal(tools.to_datetime(values)) except Exception: return values if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) elif isinstance(values, np.datetime64): return _dt_to_float_ordinal(tslibs.Timestamp(values)) elif isinstance(values, pydt.time): return dates.date2num(values) elif (is_integer(values) or is_float(values)): return values elif isinstance(values, compat.string_types): return try_parse(values) elif isinstance(values, (list, tuple, np.ndarray, Index, ABCSeries)): if isinstance(values, ABCSeries): # https://github.com/matplotlib/matplotlib/issues/11391 # Series was skipped. Convert to DatetimeIndex to get asi8 values = Index(values) if isinstance(values, Index): values = values.values if not isinstance(values, np.ndarray): values = com.asarray_tuplesafe(values) if is_integer_dtype(values) or is_float_dtype(values): return values try: values = tools.to_datetime(values) if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] except Exception: values = _dt_to_float_ordinal(values) return values
Example #26
Source File: test_frequencies.py From coffeegrindsize with MIT License | 5 votes |
def test_non_datetimeindex(self): dates = to_datetime(['1/1/2000', '1/2/2000', '1/3/2000']) assert frequencies.infer_freq(dates) == 'D'
Example #27
Source File: _converter.py From twitter-stock-recommendation with MIT License | 5 votes |
def time2num(d): if isinstance(d, compat.string_types): parsed = tools.to_datetime(d) if not isinstance(parsed, datetime): raise ValueError('Could not parse time {d}'.format(d=d)) return _to_ordinalf(parsed.time()) if isinstance(d, pydt.time): return _to_ordinalf(d) return d
Example #28
Source File: datetimes.py From vnpy_crypto with MIT License | 5 votes |
def _generate_regular_range(start, end, periods, freq): if isinstance(freq, Tick): stride = freq.nanos if periods is None: b = Timestamp(start).value # cannot just use e = Timestamp(end) + 1 because arange breaks when # stride is too large, see GH10887 e = (b + (Timestamp(end).value - b) // stride * stride + stride // 2 + 1) # end.tz == start.tz by this point due to _generate implementation tz = start.tz elif start is not None: b = Timestamp(start).value e = b + np.int64(periods) * stride tz = start.tz elif end is not None: e = Timestamp(end).value + stride b = e - np.int64(periods) * stride tz = end.tz else: raise ValueError("at least 'start' or 'end' should be specified " "if a 'period' is given.") data = np.arange(b, e, stride, dtype=np.int64) data = DatetimeIndex._simple_new(data, None, tz=tz) else: if isinstance(start, Timestamp): start = start.to_pydatetime() if isinstance(end, Timestamp): end = end.to_pydatetime() xdr = generate_range(start=start, end=end, periods=periods, offset=freq) dates = list(xdr) # utc = len(dates) > 0 and dates[0].tzinfo is not None data = tools.to_datetime(dates) return data
Example #29
Source File: _converter.py From recruit with Apache License 2.0 | 5 votes |
def time2num(d): if isinstance(d, compat.string_types): parsed = tools.to_datetime(d) if not isinstance(parsed, datetime): raise ValueError('Could not parse time {d}'.format(d=d)) return _to_ordinalf(parsed.time()) if isinstance(d, pydt.time): return _to_ordinalf(d) return d
Example #30
Source File: test_frequencies.py From recruit with Apache License 2.0 | 5 votes |
def test_non_datetimeindex(self): dates = to_datetime(['1/1/2000', '1/2/2000', '1/3/2000']) assert frequencies.infer_freq(dates) == 'D'