简体   繁体   English

Pandas, TypeError: an integer is required (get type str)

[英]Pandas, TypeError: an integer is required (got type str)

I am trying to filter my Column New_Date.我正在尝试过滤我的列 New_Date。

0    2019-11-20
1    2019-11-18
2    2019-11-16
3    2019-11-13
4    2019-11-11
5    2019-11-10
6    2019-11-08
7    2019-11-06
8    2019-11-02
9    2019-11-01
10   2019-10-30
11   2019-10-28
12   2019-10-26
13   2019-10-01
14          NaT
15          NaT
16   2019-10-18
17   2019-10-13
18   2019-10-10
19   2019-10-08
20          NaT
21          NaT


filterdate = datetime.date(input())
d1 = d1[(d1['New_Date'] > '{filterdate}') & (d1['New_Date'] != 'NaT')]

I input my date in this format "2019-11-11"我以这种格式输入我的日期“2019-11-11”

and I get this TypeError: an integer is required (got type str)我得到这个TypeError: an integer is required (got type str)

Any help would be appreciated.任何帮助,将不胜感激。

The problem with your code is with this line:您的代码的问题在于这一行:

filterdate = datetime.date(input())

The datetime.date() function is actually not able to parse the date correctly and is giving you this TypeError. datetime.date() function 实际上无法正确解析日期,并给你这个 TypeError。

Method 1方法一

What you can try is this:您可以尝试的是:

date = str(input()) year, month, day = map(int, date.split('-')) filterdate = datetime.date(year, month, day)

and then you can compare it like this:然后你可以像这样比较它:

d1 = d1[(d1['New_Date'] > filterdate) & (d1['New_Date'] != 'NaT')]

Method 2方法二

The other way to get around this problem is by using the datetime.date.fromisoformat() function.解决此问题的另一种方法是使用 datetime.date.fromisoformat() function。

filterdate = datetime.date.fromisoformat(input())

and then,接着,

d1 = d1[(d1['New_Date'] > filterdate) & (d1['New_Date'] != 'NaT')]

More details about datetime can be found here: https://docs.python.org/3/library/datetime.html有关日期时间的更多详细信息,请参见: https://docs.python.org/3/library/datetime.html

If i'm understanding this correctly, you can try:如果我理解正确,您可以尝试:

filterdate = input("Input Date in format ((Year-Month-Day): ")
d1[(d1['Date'] > filterdate) & (d1['Date'] != 'NaT')]

Inputing "2019-11-11" without the quotes (using data from another of your posts)在不带引号的情况下输入“2019-11-11”(使用您其他帖子中的数据)

Input Date in format Year-Month-Day: 2019-11-10
#Out[2203]: 
#   Date        OPP    Result
#0  2019-11-16  @DAL   L110-102
#1  2019-11-13  @POR   W114-106
#2  2019-11-11  @LAC   L98-88

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

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