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

Every line of 'convert pandas column 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

267def _convert_datetime_type(self, column):
268 """The Avro timestamp logical type is used to represent
269 MySQL datetime type, and it chooses timestamp-millis or
270 timestamp-micros depending on the fsp value.
271 """
272 metadata = self._get_primary_key_metadata(column.primary_key_order)
273 metadata[AvroMetaDataKeys.DATETIME] = True
274 fsp = column.type.fsp
275 metadata.update(self._get_fsp_metadata(fsp))
276
277 return self._builder.create_string(), metadata
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
552def detect_datetime_columns(data_frame):
553 """
554 Given a data frame traverse the columns and those that have type "string"
555 try to see if it is of type datetime. If so, apply the translation.
556 :param data_frame: Pandas dataframe to detect datetime columns
557 :return:
558 """
559 # Strip white space from all string columns and try to convert to
560 # datetime just in case
561 for x in list(data_frame.columns):
562 if data_frame[x].dtype.name == 'object':
563 # Column is a string!
564 data_frame[x] = data_frame[x].str.strip()
565
566 # Try the datetime conversion
567 try:
568 series = pd.to_datetime(data_frame[x],
569 infer_datetime_format=True)
570 # Datetime conversion worked! Update the data_frame
571 data_frame[x] = series
572 except ValueError:
573 pass
574 return data_frame
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)
302def to_pandas(self) -> None:
303 '''Return a Pandas Index.
304 '''
305 raise NotImplementedError('Pandas does not support a year month type, and it is ambiguous if a date proxy should be the first of the month or the last of the month.')
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()
2def convert_columns_dtype(df, old_dtype, new_dtype):
3 """
4 Parameters
5 ----------
6 df: pandas.DataFrame
7
8 old_dtype: numpy dtype
9
10 new_dtype: numpy dtype
11 """
12 changed = []
13 for column in df.columns:
14 if df[column].dtype == old_dtype:
15 df[column] = df[column].astype(new_dtype)
16 changed.append(column)
17
18 return changed
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)
8def datetimes_from_ts(column):
9 return column.map(
10 lambda datestring: datetime.fromtimestamp(int(datestring), tz=pytz.utc))

Related snippets