Compare timestamp with datetime - python

I have one timestamp from a dataframe and a datetime object, I want to compare them to do a select in a dataframe. My data are as followed:
print(type(datetime.datetime.now()))
<class 'datetime.datetime'>
print(type((df.created_at[0])))
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
How can I select specific rows within that dataframe with the datetime object? as follow:
df[df.created > datetime.datetime.now()]
But it returns me the following error message: TypeError: Cannot compare tz-naive and tz-aware datetime-like objects, any idea on how to solve that? thanks!

Timestamp is a timezone-aware object, while the datetime object you get from datetime.datetime.now() is timezone-naive since you don't specify otherwise, hence the error. You should convert so that they're either both timezone-aware or both timezone-naive.
For example, you can call datetime.datetime.now() like this to make it timezone-aware (passing timezone info from timestamp object as an argument):
datetime.datetime.now(df.created_at[0].tzinfo)

df[df.created.to_pydatetime() > datetime.datetime.now()]
Should work https://pandas.pydata.org/pandas-docs/version/0.22.0/generated/pandas.Timestamp.to_pydatetime.html#pandas.Timestamp.to_pydatetime

Related

How to convert str time to timestamp?

I have a string "15:15:00"
I need to convert it to timestamp like 1410748201
Python
A UNIX timestamp always needs a relation between a date and a time to actually form it, since it is the number of seconds that are passed since 1970-01-01 00:00. Therefore I would recommend you to use the datetime module.
Let's assume you have given the following format: "2022-10-31 15:15:00".
With datetime you can convert the string into a datetime object by using the strptime() function.
Afterwards, a datetime object gives you the ability to convert your datetime into a UNIX timestamp with the timestamp() method of your datetime object.
from datetime import datetime
datetime_str = "2022-10-31 15:15:00"
datetime_obj = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
print(datetime_obj.timestamp())

Datetime conversion generic approach

I have the following dates:
4/29/2020
5/7/2020
9/10/2020
10/5/2020
11/20/2020
The dates extracted from Oracle are correctly read as datetime objects in Python. However, when I manually add dates to the list in Excel, the program sees the date as string and crashes.
invalid input for query argument $1: '9/10/2020' (expected a datetime.date or datetime.datetime instance, got 'str')
This is what I am doing:
if isinstance(my_date,str):
my_date = date.fromisoformat(my_date)
It's not working. Is there a way to automatically convert a date in any format to datetime object? Thanks!
You can convert your code to something like this:
from datetime import datetime
if isinstance(my_date,str):
my_date = datetime.strptime(my_date, '%m/%d/%Y')
Yes there is : datetime.strptime
You can find documentation on how to use it here : https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

How to convert DateTime object to SQL TIMESTAMP in python

I have a datetime object that I want to INSERT in an SQL database TIMESTAMP column.
However, the procedure always fails due to formatting mismatch.
The datetime object:
date = datetime.datetime.strptime('2021-07-21 00:00:00', '%Y-%m-%d %H:%M:%S')
I tried to format it this way:
datetime.datetime.timestamp(date)
But it didn't work out, I think because the timestamp() function returns a float.
Is there a way to properly convert the datetime object to timestamp?

How to change the format of the string representation of a pandas datetime index?

I have a pandas dataframe with a datetime index. I want to change the string representation of the index to remove seconds from the end of timestamp. I'm aware I can change the datetime index into strings of the desired format using strftime, but then it will no longer be a datetime index. E.g.
df.index.strftime('%Y-%m-%d %H:%M')
I'm aware that a pandas datetime index has a method called format, which accepts a "formatter" as an input, but I can't locate any information in the documentation as to what a formatter should look like. I've tried using strftime style strings, but it gives an error. E.g.
df.index.format(formatter='%Y-%m-%d %H:%M')
TypeError: 'str' object is not callable
Any ideas?

Django datetime.date to timestamp converstion

I am having problem getting some datetime conversions. I am actually using django and in one of model field I used date field not datetime field. Now I need to show time difference and I got the snippet for that from djangosnippets. But that accepts timestamp.
So I am confuse that what actually is python timestamp and how can it be made. I tried different things as in below lines:
publish_date.timetuple()
but this says, that 'datetime.date' object has no attribute 'mktime'.I guess datetime.datetime has this mktime but not datetime.date so I can't use that.
int(parse(publish_date).strftime('%s'))
Here parse is from a third party module named dateutil but this time it gives following error.
datetime.date' object has no attribute 'read'
And it gives same error for following code:
humanizeTimeDiff(publish_date)
So any better idea or approach that I should try to convert datetime.date object to timestamp. It seems like converting datetime.datetime would be also helpful. But how ?
Since you don't have any time information in your database (only date), you can only calculate the difference in number of days. To do that, simply subtract the date objects. This will yield a timedelta object which represents the difference between two date (or datetime) objects.
>>> d1
datetime.date(2012, 9, 16)
>>> d2
datetime.date(2012, 9, 17)
>>> d2-d1
datetime.timedelta(1)
>>> print d2-d1
1 day, 0:00:00
>>> print (d2-d1).days
1

Categories