excelguy
excelguy

Reputation: 1624

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

I am trying to filter my Column 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"

and I get this TypeError: an integer is required (got type str)

Any help would be appreciated.

Upvotes: 1

Views: 9294

Answers (2)

Sarques
Sarques

Reputation: 463

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.

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.

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

Upvotes: 2

oppressionslayer
oppressionslayer

Reputation: 7224

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)

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

Upvotes: 1

Related Questions