Получить секунды из отметки времени в Python-Pandas
Опубликовано: 27 Марта, 2022
In this program our task is to create a program in python which will extract seconds from a given timestamp. We will use the s attribute of the DataTime object to extract the second.
Пример: если у нас есть фрейм данных, содержащий 5 различных отметок времени, например:
Отметка времени 2012-12-11 04:12:01 2013-10-13 04:12:04 2014-12-14 04:12:05 2015-11-15 04:12:06 2016-10-15 04:12:07
И наша задача - извлечь эти секунды из заданных временных меток. Итак, вывод здесь будет:
секунды 1 4 5 6 7
Example 1 :
# importing the moduleimport pandas as pd # generating 10 timestamp starting from "2016-10-10 09:21:12"date1 = pd.Series(pd.date_range("2016-10-10 09:21:12", periods = 10, freq = "s")) # coverting pandas series into data framedf = pd.DataFrame(dict(GENERATEDTIMESTAMP = date1)) # extracting seconds from time stampdf["extracted_seconds_timestamp"] = df["GENERATEDTIMESTAMP"].dt.second # displaying DataFramedisplay(df) |
Output :
Example 2 :
# importing the moduleimport pandas as pd # generating 5 timestamp starting from "2020-01-01 00:00:00"date1 = pd.Series(pd.date_range("2020-01-01 00:00:00", periods = 5, freq = "s")) # coverting pandas series into data framedf = pd.DataFrame(dict(GENERATEDTIMESTAMP = date1)) # extracting seconds from time stampdf["extracted_seconds_timestamp"] = df["GENERATEDTIMESTAMP"].dt.second # displaying DataFramedisplay(df) |
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course