📌  相关文章
📜  在 Pandas Dataframe 中将一系列日期字符串转换为时间序列

📅  最后修改于: 2022-05-13 01:54:25.975000             🧑  作者: Mango

在 Pandas Dataframe 中将一系列日期字符串转换为时间序列

在数据集的分析过程中,通常会发生日期没有以正确的类型表示,而是以简单的字符串形式出现,这使得处理它们并对其执行标准的日期时间操作变得困难。

pandas.to_datetime() 函数有助于将日期字符串转换为Python日期对象。因此,它可用于将一系列日期字符串转换为时间序列。

让我们看一些例子:
示例 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))


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))


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))


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))


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))


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))


输出:

日期字符串到时间序列的转换

示例 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))

输出:

日期字符串到时间序列的转换-1

示例 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))

输出:

日期字符串到时间序列的转换-2

示例 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))

输出:

日期字符串到时间序列的转换-3

示例 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))

输出:

日期字符串到时间序列的转换-4

示例 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))

输出:

日期字符串到时间序列的转换-5