如何在 Pandas DataFrame 中将整数转换为日期时间?
让我们讨论如何在其中将 Integer 转换为 Datetime。现在要在 Pandas DataFrame 中将整数转换为日期时间,我们可以使用以下语法:
df[‘DataFrame Column’] = pd.to_datetime(df[‘DataFrame Column’], format=specify your format)
注意:整数数据必须与指定的格式匹配。
示例 #1:
Python
# importing pandas package
import pandas as pd
# creating a dataframe
values = {'Dates': [20190902, 20190913, 20190921],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# display
print(df)
print(df.dtypes)
Python
# importing pandas package
import pandas as pd
# creating the dataframe
values = {'Dates': [20190902, 20190913, 20190921],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# converting the integers to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d')
# display
print(df)
print(df.dtypes)
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [190902, 190913, 190921],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing the integer dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%y%m%d')
# display
print(df)
print(df.dtypes)
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [20190902093000, 20190913093000, 20190921200000],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing integer values to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S')
# display
print(df)
print(df.dtypes)
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [20190902093000912, 20190913093000444,
20190921200000009],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing the integer dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S%F')
# display
print(df)
print(df.dtypes)
输出:
正如我们所见,“日期”列的数据类型是整数。现在要将其转换为日期时间,我们使用前面提到的语法。由于在此示例中日期格式为yyyymmdd ,因此日期格式可以表示如下:
format= '%Y%m%d'
对于我们的示例,将整数转换为 DateTime 的完整代码是:
Python
# importing pandas package
import pandas as pd
# creating the dataframe
values = {'Dates': [20190902, 20190913, 20190921],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# converting the integers to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d')
# display
print(df)
print(df.dtypes)
输出:
示例 #2:现在,假设 Dataframe 有一个格式为yymmdd的日期。在这种情况下,日期格式现在将包含小写的“y” :
format='%y%m%d'
所以完整的Python代码如下所示:
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [190902, 190913, 190921],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing the integer dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%y%m%d')
# display
print(df)
print(df.dtypes)
输出:
示例 #3:现在,假设您的整数同时包含日期和时间。在这种情况下,您应该指定的格式是:
format='%Y%m%d%H%M%S'
所以完整的Python代码将是:
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [20190902093000, 20190913093000, 20190921200000],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing integer values to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S')
# display
print(df)
print(df.dtypes)
输出:
示例 #4:考虑这个 DataFrame 在我们的 DateTime 值中包含微秒。在这种情况下,格式应指定为:
format='%Y%m%d%H%M%S%F'
所以完整的Python代码将是:
Python
# importing pandas package
import pandas as pd
# creating dataframe
values = {'Dates': [20190902093000912, 20190913093000444,
20190921200000009],
'Attendance': ['Attended', 'Not Attended', 'Attended']
}
df = pd.DataFrame(values, columns=['Dates', 'Attendance'])
# changing the integer dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S%F')
# display
print(df)
print(df.dtypes)
输出: