Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python strTime正在从月份删除0,但%m未识别_Python_Python 3.x_Pandas_Numpy_Datetime - Fatal编程技术网

Python strTime正在从月份删除0,但%m未识别

Python strTime正在从月份删除0,但%m未识别,python,python-3.x,pandas,numpy,datetime,Python,Python 3.x,Pandas,Numpy,Datetime,下面是我在数据帧上运行的代码 def get_day_limit(self): self.df['day_end'] = np.where(self.df.index == self.df.index.get_loc(dt.strptime(str(self.df.index.date),'%Y-%m-%d')+' '+'15:15:00'),1,0) 索引日期和时间格式如下所示 2020-02-18 09:15:00 2020-02-18 09:30:00 2020-02-18

下面是我在数据帧上运行的代码

def get_day_limit(self):
    self.df['day_end'] = np.where(self.df.index == self.df.index.get_loc(dt.strptime(str(self.df.index.date),'%Y-%m-%d')+' '+'15:15:00'),1,0)
索引日期和时间格式如下所示

2020-02-18 09:15:00

2020-02-18 09:30:00

2020-02-18 09:45:00
当时间等于15:15:00时,不管日期如何,我希望self.df['day\u end']为1或0

但我得到了以下错误:-

> ValueError: time data '[datetime.date(2020, 2, 18) datetime.date(2020,
> 2, 18)\n datetime.date(2020, 2, 18) ... datetime.date(2020, 5, 20)\n
> datetime.date(2020, 5, 20) datetime.date(2020, 5, 20)]' does not match
> format '%Y-%m-%d'
当我单独尝试时,我注意到0已从month datetime.date中删除,因此无法读取带有%m的month

我搜索堆栈溢出,发现当我们使用带有连字符的%Y-%m-%d格式时,它会删除开头的零。在这里

但我的数据将只能使用连字符

我尝试使用.date,但ndarray是不可调用的对象

def get_day_limit(df):
    if (df.index.hour==15) & (df.index.minute==15):
       df['day_end'] = 1
    else:
       df['day_end'] = 0


在您的情况下,您可以使用时间提取时间并比较:

# NOT the common `from datetime import datetime`
import datetime

def get_day_limit(self):
    self.df['day_end'] = (self.df.index.time == datetime.time(15,15)).astype(int)
或者没有额外包装的其他方式:

def get_day_limit(self):
    time = self.df.index - self.df.index.normalize()
    self.def['day_end'] = (time==pd.to_timedelta('15:15:00')).astype(int)

因为您可能已经将整个系列传递给了您的函数,但是您没有显示打印self.df.index得到了什么?DatetimeIndex['2020-02-18 09:15:00','2020-02-18 09:30:00','2020-02-18 09:45:00','2020-02-18 10:00:00'],dtype='datetime64[ns]',name='Date length=1495,freq=Nonei确实添加了整个系列,但np在逐行迭代时,它给了我以下错误AttributeError:“DatetimeIndex”对象没有属性“hours”@kunthudhada应该只是df['day_end']=s.hour==15&s.minute=15。这段代码对我来说很有用。我将小时改为小时,分钟改为分钟,非常感谢。这给了我以下错误类型错误:描述符“time”需要一个“datetime.datetime”对象,但收到一个“int”@kunthudhada请尝试第二种方法,或者我在另一个答案下的评论。另外,它只是导入datetime,而不是从datetime导入datetime。它们是两个不同的东西。@Kunthudhada:Quanghaang的解决方案对我来说很好,你的问题似乎与df.index有关。在类中调用该方法时,请确保它是dtype='datetime64[ns]'