AttributeError:“ datetime.datetime”对象没有属性“ striftime”

时间:2019-04-28 01:34:08

标签: python-3.x openweathermap

我目前正在为学校编写机器学习程序,以预测天气。我一直以这篇文章https://stackabuse.com/using-machine-learning-to-predict-the-weather-part-1/作为我的主要资源(由于wunderground不再是免费的,因此我不得不进行调整,因此我改用了openweathermap)。我在编写代码的数据收集和组织部分时收到以下错误'AttributeError:'datetime.datetime'对象没有属性'striftime'。预先对大量的代码块感到抱歉,我认为这将是解决问题的最佳方法。感谢您的帮助。带有“ **代码**”的部分是我正在努力解决的问题

from datetime import datetime
from datetime import timedelta
import time
from collections import namedtuple
import pandas as pd
import requests
import matplotlib.pyplot as plt

#Data collection and Organization
url = 'http://history.openweathermap.org//storage/d12a3df743e650ba4035d2c6d42fb68f.json'

#res = requests.get(url)

#data = res.json()

target_date = datetime(2018, 4, 22)
features = ["date", "temperature", "pressure", "humidity", "maxtemperature", "mintemperature"]
DailySummary = namedtuple("DailySummary", features)

def extra_weather_data(url, target_date, days):
    for _ in range(days):
        **request = url.format(target_date.striftime('%Y%m%d'))**
        respone = requests.get(request)
        if response.status_code == 200:
            data = response.json()
            records.append(DailySummary(
                date = target_date,
                temperature = data['main']['temp'],
                pressure = data['main']['pressure'],
                humidity = data['main']['humidity'],
                maxtemperature = data['main']['temp_max'],
                mintemperature = data['main']['temp_min']))
        time.sleep(6)
        target_date += timedelta(days=1)

**records = extra_weather_data(url, target_date, 365)**
#Finished data collection now begin to clean and process data using Pandas
df = pd.DataFrame(records, columns=features).set_index('date')
tmp = df[['temperature','pressure','humidty', 'maxtemperature', 'mintemperature']].head(10)

def derive_nth_day_feature(df, feature, N):
    rows =df.shape[0]
    nth_prior_measurements = [None]*N + [df[feature][i-N] for i in range(N,rows)]
    col_name = "{}_{}".format(feature, N)
    df[col_name] = nth_prior_measurements

for feature in features:
    if feature != 'date':
        for N in range(1, 4):
            derive_nth_day_feature(df, feature, N)
df.columns

0 个答案:

没有答案