10 examples of 'pandas timestamp to datetime' in Python

Every line of 'pandas timestamp 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
this disclaimer
30def converter(df: DataFrame) -> Series:
31 return df[field_name]
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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()
50def timestamp_to_datetime(ts):
51 """
52 Convert timestamps to datetime objects
53 """
54 if ts is None:
55 return None
56 if isinstance(ts, (str, bytes)):
57 ts = float(ts)
58 return datetime.datetime.utcfromtimestamp(ts)
7def timestamp_to_datetime(s):
8 """
9 Convert a Core Data timestamp to a datetime. They're all a float of seconds since 1 Jan 2001. We calculate
10 the seconds in offset.
11 """
12 if not s:
13 return None
14
15 OFFSET = (datetime.datetime(2001, 1, 1, 0, 0, 0) - datetime.datetime.fromtimestamp(0)).total_seconds()
16 return datetime.datetime.fromtimestamp(s + OFFSET)
16def timestamp2datetime(timestamp: int):
17 date = datetime.datetime.fromtimestamp(timestamp / 1000)
18 return date
83def datetime_to_timestamp(dt_):
84 """Convert given datetime object to timestamp in seconds."""
85 return dt_.replace(tzinfo=dt.timezone.utc).timestamp()
605def datetime_to_timestamp(dt):
606 if isinstance(dt, datetime.datetime):
607 return calendar.timegm(dt.utctimetuple())
608 return dt
322def to_timestamp(self, freq=None, how='start'):
323 """
324 Cast to DatetimeArray/Index.
325
326 Parameters
327 ----------
328 freq : string or DateOffset, optional
329 Target frequency. The default is 'D' for week or longer,
330 'S' otherwise
331 how : {'s', 'e', 'start', 'end'}
332
333 Returns
334 -------
335 DatetimeArray/Index
336 """
337 from pandas.core.arrays import DatetimeArray
338
339 how = libperiod._validate_end_alias(how)
340
341 end = how == 'E'
342 if end:
343 if freq == 'B':
344 # roll forward to ensure we land on B date
345 adjust = Timedelta(1, 'D') - Timedelta(1, 'ns')
346 return self.to_timestamp(how='start') + adjust
347 else:
348 adjust = Timedelta(1, 'ns')
349 return (self + self.freq).to_timestamp(how='start') - adjust
350
351 if freq is None:
352 base, mult = libfrequencies.get_freq_code(self.freq)
353 freq = libfrequencies.get_to_timestamp_base(base)
354 else:
355 freq = Period._maybe_convert_freq(freq)
356
357 base, mult = libfrequencies.get_freq_code(freq)
358 new_data = self.asfreq(freq, how=how)
359
360 new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base)
361 return DatetimeArray._from_sequence(new_data, freq='infer')
32def datetime2timestamp(dt=None, timezone='Asia/Shanghai'):
33 if dt is None:
34 dt = datetime.now()
35 tz = pytz.timezone(timezone)
36 dt = dt.replace(tzinfo=pytz.utc).astimezone(tz)
37 return calendar.timegm(dt.timetuple())

Related snippets