You would use
df['datetime_colum'].apply(lambda x: x.toordinal())
If it fails, the cause could be that your column is an object and not datetime. So you need:
df['datetime_colum'] = pd.to_datetime(df['datetime_colum'])
Use a numpy.dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types.,Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).,Deprecated since version 1.3.0: Using astype to convert from timezone-naive dtype to timezone-aware dtype is deprecated and will raise in a future version. Use Series.dt.tz_localize() instead.,Cast a pandas object to a specified dtype dtype.
>>> d = {
'col1': [1, 2],
'col2': [3, 4]
} >>>
df = pd.DataFrame(data = d) >>>
df.dtypes
col1 int64
col2 int64
dtype: object
>>> df.astype('int32').dtypes
col1 int32
col2 int32
dtype: object
>>> df.astype({
'col1': 'int32'
}).dtypes
col1 int32
col2 int64
dtype: object
>>> ser = pd.Series([1, 2], dtype = 'int32') >>>
ser
0 1
1 2
dtype: int32 >>>
ser.astype('int64')
0 1
1 2
dtype: int64
>>> ser.astype('category')
0 1
1 2
dtype: category
Categories(2, int64): [1, 2]
>>> from pandas.api.types
import CategoricalDtype
>>>
cat_dtype = CategoricalDtype(
...categories = [2, 1], ordered = True) >>>
ser.astype(cat_dtype)
0 1
1 2
dtype: category
Categories(2, int64): [2 < 1]
Let us see how to convert integer columns to datetime by using Python Pandas.,In this Python tutorial, we will learn how to convert Integers to Datetime in Pandas DataFrame. Also, we will cover these topics.,In this section, we will discuss how to convert datetimeindex with an integer in Pandas Dataframe by using Python.,Let’s take an example and check how to convert integers to datetime in Pandas Dataframe by using Python
Here is the Syntax of Pandas.Datetime() method
Pandas.to_datetime(
arg,
errors = 'raise',
dayfirst = 'False',
yearfirst = 'False',
utc = None,
format = None,
exact = True,
unit = None,
infer_datetime_format = false,
origin = 'unix',
cache = True
)
Let’s take an example and check how to convert integers to datetime in Pandas Dataframe by using Python
import pandas as pd
new_dict = {
'no_dates': [19990320, 20200416, 20010828],
'new_stats': ['Available', 'Available', 'Available']
}
df = pd.DataFrame(new_dict, columns = ['no_dates', 'new_stats'])
df['no_dates'] = pd.to_datetime(df['no_dates'], format = '%Y%m%d')
print(df)
Here is the Syntax of pd.to_datetime() method
Pandas.to_datetime(
arg,
errors = 'raise',
dayfirst = 'False',
yearfirst = 'False',
utc = None,
format = None,
exact = True,
unit = None,
infer_datetime_format = false,
origin = 'unix',
cache = True
)
Example:
import pandas as pd
new_val = 20100820
new_dt = pd.DataFrame({
'date': [new_val] * 3
})
new_dt['New_DateTime'] = new_dt['date'].apply(lambda m: pd.to_datetime(str(m), format = '%Y%m%d'))
print(new_dt)
Here is the Syntax of xldate_as_datetime
xlrd.xldate_as_datetime()