简体   繁体   English

TypeError:strptime()参数1必须是str,而不是list

[英]TypeError: strptime() argument 1 must be str, not list

I am trying to plot the following 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
................
05/01/2012 00:00
05/01/2012 01:00
................ 

Against wind_speed data which is in the format: 针对以下格式的wind_speed数据:

 [ 3.30049159  2.25226244  1.44078451 ... 12.8397099   9.75722427
  7.98525797]

My code is: 我的代码是:

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

However, I am receiving the error message "TypeError: strptime() argument 1 must be str", not list. 但是,我收到错误消息“ TypeError:strptime()参数1必须为str”,而不是列表。 I am new to Python and would appreciate help on how to either convert to the list to a string or another method on how to resolve this. 我是Python的新手,希望能对如何将列表转换为字符串或如何解决此问题的其他方法有所帮助。 Thank you! 谢谢!

As the other answers suggest, you require a different way, probably map . 正如其他答案所建议的那样,您需要使用其他方法,可能是map You may also use pd.to_datetime() and pass it the whole list. 您也可以使用pd.to_datetime()并将其传递给整个列表。 And then use the same as x-axis and the wind_speed for y-axis. 然后使用与x轴相同的值,将wind_speed用作y轴。

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

It will create a DatetimeIndex which you can again format according to your needs, like: 它将创建一个DatetimeIndex,您可以根据自己的需要再次格式化它,例如:

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

In one line: 一行:

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

After having two lists for timestamp and wind_speed, use may use something like: 在有两个时间戳和wind_speed列表之后,use可以使用类似以下内容:

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

This should help. 这应该有所帮助。 Using map with lambda to convert datetime to your required format. 使用带lambda的map将日期时间转换为所需的格式。

Demo: 演示:

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)

Output: 输出:

['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']

Your variable T is apparently a list of strings, not a string itself, so you need to iterate through T and pass the items in T to strptime instead. 变量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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `TypeError:strptime()参数0必须是str,而不是 <class 'bytes'> ` - `TypeError: strptime() argument 0 must be str, not <class 'bytes'>` 类型错误:strptime() 参数 1 必须是 str,而不是句点 - TypeError: strptime() argument 1 must be str, not Period 类型错误:strptime() 参数 1 必须是 str,而不是 None - TypeError : strptime() argument 1 must be str, not None TypeError: strptime() 参数 1 必须是 str,而不是 Series - TypeError: strptime() argument 1 must be str, not Series Datetime(TypeError:strptime() 参数 1 必须是 str,而不是 Timestamp) - Datetime (TypeError: strptime() argument 1 must be str, not Timestamp) datetime.strptime:TypeError:strptime()参数1必须是str,而不是Series - datetime.strptime: TypeError: strptime() argument 1 must be str, not Series 将loadtxt列转换为工作日:TypeError:strptime()参数1必须为str,而不是字节 - Converting a loadtxt column to a weekday: TypeError: strptime() argument 1 must be str, not bytes TypeError:strptime()参数1必须是str,而不是datetime.date Python - TypeError: strptime() argument 1 must be str, not datetime.date Python TypeError: strptime() argument 1 must be str, not float 不明白为什么会这样 - TypeError: strptime() argument 1 must be str, not float do not understand why this is happening strptime() 参数 1 必须是 str,而不是 Series - strptime() argument 1 must be str, not Series
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM