How to convert DateTime object to SQL TIMESTAMP in python - 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?

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

Convert datetime.date or string to timestamp in python

I am aware that this question was posted more times before but yet I have few doubts. I have a datetime.date (ex. mydate = date(2014,5,1)) and I converted this as a string, then saved in DB as a column (dtype:object) in a table. Now I wanted to change the storage of dates from text to timestamp in DB. I tried this,
Ex. My table is tab1. I read this as dataframe df in python.
# datetime to timestamp
df['X'] = pd.to_datetime(mydate)
When I check dtype in python editor df.info(), the dtype of X is datetime64[ns] but when I save this to DB in MySQL and read again as dataframe in python, the dtype changes as object. I have datatype as datetime in MySQL but I need this as timestamp datatype in MySQL. Is there any way to do it? Also, I need only date from Timestamp('2014-5-01 00:00:00') and exclude time.
The problem is that when u read the serialized value from MySQL the python MySQL connector does not convert it. you have to convert it to DateTime value after reading data from the cursor by calling your function again on retrieved data:
df['X'] = pd.to_datetime(df['col'])
As suggested, I changed the column type directly by using dtype argument in to_sql() function while inserting into the database. So, now I can have datatypes as TIMESTAMP, DATETIME and also DATE in MySQL.
such works for me:
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df['timestamp'] = df['Date'].apply(lambda x: x.timestamp())

Compare timestamp with datetime

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

Converting string timestamp to datetime using pyarrow

Is there any possibility to convert string timestamp in pyarrow table to datetime format before writing to parquet file?
Depending on the timestamp format, you can make use of pyarrow.compute.strptime function. It is not well-documented yet, but you can use something like this:
import pyarrow.compute as pc
pc.strptime(table.column("Timestamp"), format='%Y-%m-%d %H:%M:%S', unit='s')
provided your data is stored in table and "Timestamp" is the name of the column with timestamp strings.

Categories