【问题标题】:TypeError: strptime() argument 1 must be str, not list类型错误:strptime() 参数 1 必须是 str,而不是列表
【发布时间】:2018-06-27 21:29:46
【问题描述】:

我正在尝试绘制以下数据:

01/01/2012 01:00
01/01/2012 02:00
01/01/2012 03:00
01/01/2012 04:00
01/01/2012 05:00
01/01/2012 06:00
01/01/2012 07:00
01/01/2012 08:00
01/01/2012 09:00
01/01/2012 10:00
01/01/2012 11:00
01/01/2012 12:00
01/01/2012 13:00
01/01/2012 14:00
01/01/2012 15:00
01/01/2012 16:00
01/01/2012 17:00
01/01/2012 18:00
01/01/2012 19:00
01/01/2012 20:00
01/01/2012 21:00
01/01/2012 22:00
01/01/2012 23:00
02/01/2012 00:00
04/01/2012 23:00
................
05/01/2012 00:00
05/01/2012 01:00
................ 

针对风速数据,格式为:

 [ 3.30049159  2.25226244  1.44078451 ... 12.8397099   9.75722427
  7.98525797]

我的代码是:

    T = T[1:]
    print( datetime.datetime.strptime(T, "%m/%d/%Y %H:%M:%S").strftime("%Y%m%d %I:%M:%S") #pharsing the time
    TIMESTAMP = [str (i) for i in T]
    plt.plot_date(TIMESTAMP, wind_speed)
    plt.show()

但是,我收到错误消息“TypeError: strptime() argument 1 must be str”,而不是列表。我是 Python 新手,希望获得有关如何将列表转换为字符串或如何解决此问题的其他方法的帮助。谢谢!

【问题讨论】:

  • T = T[1]?.....
  • @Rakesh 我正在使用 T = T[1] 跳过列中的第一个单元格
  • 是否要将列表中的所有字符串日期时间转换为日期时间对象?
  • 在 for 循环中进行转换:TIMESTAMP = [str(datetime.datetime.strptime(i, ...).strftime(...)) for i in T]
  • @Rakesh 是的,我相信是的,我是 Python 新手,我可以尝试一下,看看是否可行

标签: python datetime utc


【解决方案1】:

正如其他答案所暗示的,您需要一种不同的方式,可能是map。您也可以使用pd.to_datetime() 并将整个列表传递给它。然后用和x轴一样的,y轴用wind_speed。

import pandas as pd    
timestamp = pd.to_datetime(T[1:])

它将创建一个 DatetimeIndex,您可以根据需要再次对其进行格式化,例如:

timestamp = timestamp.strftime("%Y%m%d %I:%M:%S")

一行:

timestamp = pd.to_datetime(T[1:]).strftime("%Y%m%d %I:%M:%S")

在有两个时间戳和风速列表后,使用可能会使用类似的东西:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(13,6))
ax.plot(timestamp, wind_speed)
plt.xticks(rotation=30)
plt.show()

【讨论】:

  • 能举个例子吗?我是 Python 新手,使用它有点挑战性
  • 感谢您的帮助。我的日期和时间现在的格式是“['20120101 01:00:00' '20120101 02:00:00' '20120101 03:00:00' ... '20131130”,现在我该如何绘制此数据与原始问题中发布的 wind_speed 数据对比?
  • 一旦你有两个列表(时间戳和风速),你可以使用 matplotlib 来绘制。在上面的答案中添加代码。
  • 我已经附上了您更新的绘图功能,但是,代码不会生成图表。代码仍保留在 IN[*] 上,这可能是什么原因?
  • 这意味着它处于运行状态,原因之一可能是因为您有 16k 值。尝试从两个列表中分割前几个值(timestamp[:100] 和 wind_speed[:100])然后绘图。
【解决方案2】:

这应该会有所帮助。使用 map 和 lambda 将日期时间转换为您需要的格式。

演示:

import datetime
data = ['01/01/2012 01:00', '01/01/2012 02:00', '01/01/2012 03:00', '01/01/2012 04:00', '01/01/2012 05:00', '01/01/2012 06:00', '01/01/2012 07:00', '01/01/2012 08:00', '01/01/2012 09:00', '01/01/2012 10:00', '01/01/2012 11:00', '01/01/2012 12:00', '01/01/2012 13:00', '01/01/2012 14:00', '01/01/2012 15:00', '01/01/2012 16:00', '01/01/2012 17:00', '01/01/2012 18:00', '01/01/2012 19:00', '01/01/2012 20:00', '01/01/2012 21:00', '01/01/2012 22:00', '01/01/2012 23:00', '02/01/2012 00:00', '04/01/2012 23:00']
data = list(map(lambda x: datetime.datetime.strptime(x,  "%m/%d/%Y %H:%M").strftime("%Y%m%d %I:%M:%S"), data))
print(data)

输出:

['20120101 01:00:00', '20120101 02:00:00', '20120101 03:00:00', '20120101 04:00:00', '20120101 05:00:00', '20120101 06:00:00', '20120101 07:00:00', '20120101 08:00:00', '20120101 09:00:00', '20120101 10:00:00', '20120101 11:00:00', '20120101 12:00:00', '20120101 01:00:00', '20120101 02:00:00', '20120101 03:00:00', '20120101 04:00:00', '20120101 05:00:00', '20120101 06:00:00', '20120101 07:00:00', '20120101 08:00:00', '20120101 09:00:00', '20120101 10:00:00', '20120101 11:00:00', '20120201 12:00:00', '20120401 11:00:00']

【讨论】:

  • 在名为 data 的第二行中,我将如何包含我的整个数据列?我提供的数据是 16,000 个值的 sn-p?谢谢
  • 你的意思是data = list(map(lambda x: datetime.datetime.strptime(x, "%m/%d/%Y %H:%M").strftime("%Y%m%d %I:%M:%S"), T[1:])) 吗?
  • 我已经尝试过您的代码,但是,我收到错误“ValueError: time data '01/01/2012 02:00' does not match format 'd/%m/%Y %H: %M'" 虽然我相信我的格式是正确的?
【解决方案3】:

您的变量 T 显然是一个字符串列表,而不是字符串本身,因此您需要遍历 T 并将 T 中的项目传递给 strptime

for t in T:
    datetime.datetime.strptime(t, "%m/%d/%Y %H:%M:%S").strftime("%Y%m%d %I:%M:%S")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2018-09-04
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多