What kind of timestamp is this? Wrong conversion - python

I'm reading a file in python and I;m trying to convert the timestamp field into a readable form.
My timestamps have the following form:
174bb26825e
174bb26f5b6
174bb2641d2
174bb2705db
174bb26f390
When I use datetime.datetime.utcfromtimestamp I get a date that is in 1970 and when I convert it online in a Unix Hex Timestamp Converter I get a date that comes from the future(52699)!
My code is the following:
df['DateTime'] = df['DateTime'].apply(lambda x: datetime.datetime.utcfromtimestamp(float(int(x, 16))/16**4))
Any ideas?

Related

What's the correct datetime format for this string date generated by python?

I have this date example '2022-08-30T11:53:52.204219' stored in database, when I get it from database it's type is string so I wanted to convert it to a date type by using this python code
datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%d'T'%H:%M:%S.%f")
I also tried this one
datetime.strptime('2022-08-30T11:53:52.204219', "yyyy-MM-dd'T'HH:mm:ssZ")
But I always get this error response 'time data '2022-08-30T11:53:52.204219' does not match format "%Y-%m-%d'T'%H:%M:%S.%f'
I need help to convert this string date to an actual date
As per comment:
from datetime import datetime
print(datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%dT%H:%M:%S.%f"))
Result:
2022-08-30 11:53:52.204219

Pyspark - Convert to Timestamp

Spark version : 2.1
I'm trying to convert a string datetime column to utc timestamp with the format yyyy-mm-ddThh:mm:ss
I first start by changing the format of the string column to yyyy-mm-ddThh:mm:ss
and then convert it to timestamp type. Later I would convert the timestamp to UTC using to_utc_timestamp function.
df.select(
f.to_timestamp(
f.date_format(f.col("time"), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd'T'HH:mm:ss"
)
).show(5, False)
The date_format works fine by giving me the correct format. But, when I do to_timestamp on top of that result, the format changes to yyyy-MM-dd HH:mm:ss, when it should instead be yyyy-MM-dd'T'HH:mm:ss. Why does this happen?
Could someone tell me how I could retain the format given by date_format? What should I do?
The function to_timestamp returns a string to a timestamp, with the format yyyy-MM-dd HH:mm:ss.
The second argument is used to define the format of the DateTime in the string you are trying to parse.
You can see a couple of examples in the official documentation.
The code should be like this, just look at the single 'd' part here, and this is tricky in many cases.
data= data.withColumn('date', to_timestamp(col('date'), 'yyyy/MM/d'))

How to convert date format from dd/mm/yyyy to yyyy-mm-dd base on RIDE python robot framework

If i got the message from the webpage to 18/05/2020. I want to convert it to this format: 2020-05-18
I tried using the date of % Y-% m-% d conversion, but there was an error that the value Mismatch error
How should i do ?
You need Convert Date from DateTime library and to specify both result format and input forward:
${converted} Convert Date 18/05/2020 result_format=%Y-%m-%d date_format=%d/%m/%Y

Formatting datetime pandas

I have some rows in my dataset with the following release date format:
1995-10-30
It is an object/string. However, I want to convert it to datetime, so I wrote the following to achieve that:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date)
It gets converted to datetime as it should, but I would like to have the following format
mm-dd-year
I have tried yearfirst=False and dayfirst=False but nothing seems to be happening and I cant figure out why it isnt working.
I have also tried to specify the format in the to_datetime method as following:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date, format="%Y/%m/%d", dayfirst=False, yearfirst=False)
Any help is appriciated
You can convert datetimes to strings with format mm-dd-YY:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date).dt.strftime('%m-%d-%Y')
But if want datetimes in format mm-dd-YY it is not possible in python.

Django date format issue - does not match format

I need to change the date into the format MM/YY. The issue is I don't always don't what the date format is to begin with. It's my understanding that the code below would need to know what format the date was in first before I can change it is that correct?
newdate = datetime.datetime.strptime(str(mydate), '%Y/%m/%d').strftime('%m-%Y')
as a result I get the error:
time data '2020-09-30' does not match format '%Y/%m/%d' (
How to I convert my date to MM/YY?
I'm using Django 1.4.6
newdate = datetime.datetime.strptime(str(mydate), '%Y-%m-%d').strftime('%m/%Y')

Categories