10 examples of 'numpy datetime64 to datetime' in Python

Every line of 'numpy datetime64 to datetime' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
42def datetime2int(dt):
43 # represents a datetime as microseconds since the epoch (i think this may assume UTC if the datetime doesn't specify?)
44 return (datetime64(dt) - datetime64(0, 'us')).astype(int)
33def datesarray_to_datetimearray(dates: np.ndarray) -> np.ndarray:
34 """
35 Convert an pandas-array of timestamps into
36 An numpy-array of datetimes
37 :return: numpy-array of datetime
38 """
39 return dates.dt.to_pydatetime()
49def np_array_datetime64_compat(arr, *args, **kwargs):
50 """
51 provide compat for construction of an array of strings to a
52 np.array(..., dtype=np.datetime64(..))
53 tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
54 warning, when need to pass '2015-01-01 09:00:00'
55 """
56 # is_list_like
57 if (hasattr(arr, '__iter__')
58 and not isinstance(arr, string_and_binary_types)):
59 arr = [tz_replacer(s) for s in arr]
60 else:
61 arr = tz_replacer(arr)
62
63 return np.array(arr, *args, **kwargs)
150def utc_timestamp_to_datetime(timestamp):
151 """
152 Converts the given timestamp to a datetime instance.
153
154 :type timestamp: float
155 :rtype: datetime
156 """
157
158 if timestamp is not None:
159 return datetime.fromtimestamp(timestamp, utc)
14def datetimeAsTimestamp(dt):
15 return dt.timestamp()
553def to_datetime(self, dayfirst=False):
554 """
555 DEPRECATED: use :meth:`to_timestamp` instead.
556
557 Cast to DatetimeIndex.
558 """
559 warnings.warn("to_datetime is deprecated. Use self.to_timestamp(...)",
560 FutureWarning, stacklevel=2)
561 return self.to_timestamp()
11def _epoch_utc_to_datetime(epoch_utc):
12 """
13 Helper function for converting epoch timestamps (as stored in JWTs) into
14 python datetime objects (which are easier to use with sqlalchemy).
15 """
16 return datetime.fromtimestamp(epoch_utc)
77def to_timestamps(values):
78 try:
79 if len(values) == 0:
80 return []
81 if isinstance(values[0], (numpy.datetime64, datetime.datetime)):
82 times = numpy.array(values)
83 else:
84 try:
85 # Try to convert to float. If it works, then we consider
86 # timestamps to be number of seconds since Epoch
87 # e.g. 123456 or 129491.1293
88 float(values[0])
89 except ValueError:
90 try:
91 # Try to parse the value as a string of ISO timestamp
92 # e.g. 2017-10-09T23:23:12.123
93 numpy.datetime64(values[0])
94 except ValueError:
95 # Last chance: it can be relative timestamp, so convert
96 # to timedelta relative to now()
97 # e.g. "-10 seconds" or "5 minutes"
98 times = numpy.fromiter(
99 numpy.add(numpy.datetime64(utcnow()),
100 [to_timespan(v, True) for v in values]),
101 dtype='datetime64[ns]', count=len(values))
102 else:
103 times = numpy.array(values, dtype='datetime64[ns]')
104 else:
105 times = numpy.array(values, dtype='float') * 10e8
106 except ValueError:
107 raise ValueError("Unable to convert timestamps")
108
109 times = times.astype('datetime64[ns]')
110
111 if (times < unix_universal_start64).any():
112 raise ValueError('Timestamp must be after Epoch')
113
114 return times
8def convert_to_datetime(datetime_str):
9 tweet_datetime = datetime.strptime(datetime_str,'%a %b %d %H:%M:%S %z %Y')
10
11 return(tweet_datetime)
27def convert_date(array):
28 array[0,:,:][array[0,:,:] > 0] += 1970
29 array[0,:,:] = array[0,:,:].astype(np.int)
30 return array

Related snippets