10 examples of 'pandas convert datetime to string' in Python

Every line of 'pandas convert datetime to string' 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
45def _is_datetime(s):
46 if is_datetime64_any_dtype(s):
47 return True
48 try:
49 if is_object_dtype(s):
50 pd.to_datetime(s, infer_datetime_format=True)
51 return True
52 except Exception: # pylint: disable=broad-except
53 pass
54 return False
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

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)
148def localize_datetime(x):
149 if pd.isnull(x) or isinstance(x, pd.tslib.NaTType):
150 return None
151 utz_tz = pytz.timezone('UTC')
152 local_tz = pytz.timezone(utc_to_tz)
153 d = utz_tz.localize(x)
154 d = d.astimezone(local_tz)
155 # make naive.
156 return d.replace(tzinfo=None)
43def convert_datetime(s):
44 """
45 Convert a datetime string to a standard format
46 """
47 try:
48 dt = DateTimeFromString(s)
49 date_string = dt.Format("%m/%d/%Y %H:%M:%S")
50 return date_string
51 except:
52 return s
30def converter(df: DataFrame) -> Series:
31 return df[field_name]
11def to_datetime(date_str: str) -> datetime:
12 d = date_str
13 try:
14 dte = datetime.strptime(d, "%Y-%m")
15 except ValueError:
16 dte = date_str
17 return dte
5def parse_datetime(datetime_str):
6 if datetime_str:
7 return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
17def format_datetime(dt):
18 """Return Open-States-style datetime string from datetime object."""
19 return dt.strftime(DATETIME_TEMPLATE)
64def format_datetime(dt: datetime) -> str:
65 """
66 Format a datetime object as string.
67
68 :param dt: Date and time to format.
69 :return: String representation.
70 """
71 return dt.strftime("%Y-%m-%d %H:%M:%S%z")
70def datetime_to_text(datetime_info):
71 """
72 Serialize ``datetime``, ``date`` or ``time`` instances as text.
73
74 Args:
75 datetime_info (dt.datetime, dt.date, dt.time, None): Python datetime
76 information that is to be serialized.
77
78 Returns:
79 text_type: Returns optinionated text representation of passed datetime
80 information. ``dt.datetime`` will be represented as ``%Y-%m-%d %H:%M:%S``,
81 ``dt.date`` will be represented as ``%Y-%m-%d`` and ``dt.time`` as
82 ``%H-%M-%S``. ``None`` will be serialized as an ``empty string``.
83
84 Raises:
85 TypeError: If ``datetime_info`` is not ``datetime.datetime``,
86 ``datetime.date``, ``datetime.time`` or ``None``.
87 """
88 if isinstance(datetime_info, datetime.datetime):
89 result = datetime_info.strftime('%Y-%m-%d %H:%M:%S')
90 elif isinstance(datetime_info, datetime.date):
91 result = datetime_info.strftime('%Y-%m-%d')
92 elif isinstance(datetime_info, datetime.time):
93 result = datetime_info.strftime('%H:%M:%S')
94 elif datetime_info is None:
95 result = ''
96 else:
97 raise TypeError
98 return text_type(result)

Related snippets