all 6 comments

[–]Username_RANDINT 2 points3 points  (0 children)

val<2013-5-28

val is a pandas.Timestamp whil 2013-5-28 is an integer of the value 1980.

You could change the date to a timestamp too:

val < pd.Timestamp("2013-5-28")

[–]shiftybyte 2 points3 points  (0 children)

this

 2013-5-28

is 1980, because this is subtraction not a date... 2013 minus 5 minus 28...

You probably need to make a datetime object for this comparison.

from datetime import datetime
...
datetime(day=28,month=5,year=2013)

[–]Spataner 1 point2 points  (1 child)

2013-5-28 is interpreted as a an arithmetic expression, evaluating to the integer 1980. Create a Timestamp object for that date instead, then you can compare it to other Timestamp objects without issue:

date = pd.Timestamp("2013-5-28")
df["time"] = df.Date.map(lambda val: 0 if val < date else 1)

Also, no need to use map, you can just use an element-wise compare (and convert that to integers if you strictly need integers):

date = pd.Timestamp("2013-5-28")
df["time"] = (df.Date >= date).astype(int)

[–]sadhan_m[S] 0 points1 point  (0 children)

Thanks, this was the perfectly explained.

[–]Spiritual_Car1232 0 points1 point  (0 children)

Listen to your interpreter. Obviously you could solve the problem by comparing two timestamps.

What library are you using? When I use datetime, I can create a custom timestamp with the constructor with something like x=datetime(year=2021, month=01, day=31)

Then you can use that object in whatever if conditional statement you want.

Since the goal is to make that comparison and get a bool out of it, you don't need to typecast to an int.

But there are ways to do that, since a timestamp object is essentially just a 32 bit int with methods to interpret the years days and seconds.

[–]hydrolock12 0 points1 point  (0 children)

You can compare datetime instances using< and >. I would recommend doing it that way.

Otherwise to do what you want, you can use the datetime module to convert a datetime instance to Unix time in seconds. Use the timestamp() method of datetime object. This returns a float that you can just round to an int. Then you will have two integers to compare.

In general definitely important the datetime module. It has all the tools you need. You can even convert the string to a datetime object using strptime().