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

Every line of 'pandas convert 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
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
30def converter(df: DataFrame) -> Series:
31 return df[field_name]
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
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()
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
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)
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
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.')
653def set_index_post_series(df, index_name, drop, column_dtype):
654 df2 = df.drop("_partitions", axis=1).set_index("_index", drop=True)
655 df2.index.name = index_name
656 df2.columns = df2.columns.astype(column_dtype)
657 return df2
43def convert_input(X, columns=None, deep=False):
44 """
45 Unite data into a DataFrame.
46 Objects that do not contain column names take the names from the argument.
47 Optionally perform deep copy of the data.
48 """
49 if not isinstance(X, pd.DataFrame):
50 if isinstance(X, pd.Series):
51 X = pd.DataFrame(X, copy=deep)
52 else:
53 if columns is not None and np.size(X,1) != len(columns):
54 raise ValueError('The count of the column names does not correspond to the count of the columns')
55 if isinstance(X, list):
56 X = pd.DataFrame(X, columns=columns, copy=deep) # lists are always copied, but for consistency, we still pass the argument
57 elif isinstance(X, (np.generic, np.ndarray)):
58 X = pd.DataFrame(X, columns=columns, copy=deep)
59 elif isinstance(X, csr_matrix):
60 X = pd.DataFrame(X.todense(), columns=columns, copy=deep)
61 else:
62 raise ValueError('Unexpected input type: %s' % (str(type(X))))
63
64 X = X.apply(lambda x: pd.to_numeric(x, errors='ignore'))
65 elif deep:
66 X = X.copy(deep=True)
67
68 return X

Related snippets