Durante el análisis de un conjunto de datos, a menudo sucede que las fechas no se representan en el tipo adecuado y están más bien presentes como strings simples, lo que dificulta su procesamiento y la realización de operaciones estándar de fecha y hora en ellas.
La función pandas.to_datetime() ayuda a convertir una string de fecha en un objeto de fecha de Python. Por lo tanto, se puede utilizar para convertir una serie de strings de fechas en una serie temporal.
Veamos algunos ejemplos:
Ejemplo 1:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['28 July 2020', '16 January 2013', '29 February 2016 18:14']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after being # converted to a time series print("\nSeries of date strings after" + " being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Ejemplo 2:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['2020/07/28', '2013/01/16', '2016/02/29 18:14']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after being # converted to a time series print("\nSeries of date strings after " + "being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Ejemplo 3:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['2020-07-28', '2013-01-16', '2016-02-29 18:14']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after # being converted to a time series print("\nSeries of date strings after " + "being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Ejemplo 4:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['28/07/2020', '01/16/2013', '29/02/2016 18:14']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after being # converted to a time series print("\nSeries of date strings after " + "being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Ejemplo 5:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['20200728', '20130116', '20160229 181431']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after # being converted to a time series print("\nSeries of date strings after " + "being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Ejemplo 6:
Python3
# import pandas library import pandas as pd # create a series of date strings dt_series = pd.Series(['28 July 2020', '2013-01-16', '20160229 18:14', '5/03/2019 2215', '20151204 09:23']) # display the series initially print("Series of date strings:") print(dt_series) # display the series after # being converted to a time series print("\nSeries of date strings after " + "being converted to a timeseries:") print(pd.to_datetime(dt_series))
Producción:
Publicación traducida automáticamente
Artículo escrito por amarjeet_singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA