简体   繁体   English

'datetime.datetime' 在 python 中没有属性 'datetime'

[英]'datetime.datetime' has no attribute 'datetime' in python

hi i have long code maybe if you wanna check it out tell me, so im trying to solve this and I'm pretty sure the code is right but it keeps getting me the same error, i try everything to solve it but its the same i checked if the attribute missing but its there i don't know what i have done wrong this is my code嗨,我有很长的代码,如果你想查看它,请告诉我,所以我正在尝试解决这个问题,我很确定代码是正确的,但它不断给我带来同样的错误,我尝试了一切来解决它,但它是一样的我检查了属性是否丢失但它在那里我不知道我做错了什么这是我的代码

import pandas as pd
import geopandas as gpd
import time
import pickle
import os
import numpy as np
import xgboost
import pytz
import arcgis
#
#plotting
#'''
from IPython.display import HTML, display
import datashader as ds
from datashader import transfer_functions as tf
from datashader.colors import colormap_select, Greys9, Hot, viridis, inferno
#'''

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import NullFormatter
import matplotlib as mpl
mpl.rc('xtick', color='k')
mpl.rc('ytick', color='k')
%matplotlib inline
#'''
import datetime
from pandas import Series
from datetime import datetime, timedelta
import time
from datetime import datetime, date, time, timedelta


predTimest = pd.date_range('01/06/2017', periods=7*24, freq='H',tz='US/Mountain')
predTimest
n [28]:


predTimest


Out[28]:
DatetimeIndex(['2017-01-06 00:00:00-07:00', '2017-01-06 01:00:00-07:00',
               '2017-01-06 02:00:00-07:00', '2017-01-06 03:00:00-07:00',
               '2017-01-06 04:00:00-07:00', '2017-01-06 05:00:00-07:00',
               '2017-01-06 06:00:00-07:00', '2017-01-06 07:00:00-07:00',
               '2017-01-06 08:00:00-07:00', '2017-01-06 09:00:00-07:00',
               ...
               '2017-01-12 14:00:00-07:00', '2017-01-12 15:00:00-07:00',
               '2017-01-12 16:00:00-07:00', '2017-01-12 17:00:00-07:00',
               '2017-01-12 18:00:00-07:00', '2017-01-12 19:00:00-07:00',
               '2017-01-12 20:00:00-07:00', '2017-01-12 21:00:00-07:00',
               '2017-01-12 22:00:00-07:00', '2017-01-12 23:00:00-07:00'],
              dtype='datetime64[ns, US/Mountain]', length=168, freq='H')
prediction_time = predTimest[15]
test_df = shapefile.copy()
test_df['timestamp'] = prediction_time
test_df['allah1__27'] = shapefile.allah1__27.astype('int64')
test_df['hour'] = prediction_time.hour
test_df['weekday'] = prediction_time.weekday()
test_df['month'] = prediction_time.month
def add_join_key(df):
    df['join_key'] = df.allah1__27.map(int).map(str)+df.timestamp.map(datetime.datetime.isoformat)
    df = df.set_index('join_key')
    return df
weath_df = wdf.loc[prediction_time]
test_df = add_join_key(test_df)
weath_df = add_join_key(weath_df.reset_index())

and it give me this error它给了我这个错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-65-4714b1ff587e> in <module>
----> 1 test_df = add_join_key(test_df)
      2 weath_df = add_join_key(weath_df.reset_index())

<ipython-input-63-1a4e6a6f2c07> in add_join_key(df)
      1 def add_join_key(df):
----> 2     df['join_key'] = df.allah1__27.map(int).map(str)+df.timestamp.map(datetime.datetime.isoformat)
      3     df = df.set_index('join_key')
      4     return df

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

You have imported datetime twice at the top of your script with the last import taking precedence:您已在脚本顶部两次导入日期时间,最后一次导入优先:

import datetime
from datetime import datetime, timedelta

Therefore you should be calling:因此,您应该致电:

datetime.isoformat

You should not import datetime and also from datetime import datetime .您不应该import datetime ,也不应该from datetime import datetime Best practice would be to just import datetime and then if you want the deeper module call datetime.datetime otherwise you are creating two things with the exact same name ...obviously this would lead to confusion.最佳实践是只import datetime然后如果你想要更深的模块调用datetime.datetime否则你将创建两个具有完全相同名称的东西......显然这会导致混淆。 If you really want to import both then rename one like如果你真的想同时导入,那么重命名一个

import datetime
from datetime import datetime as datet

Now you can refer to one as datetime and the other as datet instead of having both of them refer to the same name " datetime "现在,你可以参考一个作为datetime ,另一个作为datet ,而不必两者指向同一个名字“ datetime

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM