Plotly axis shows Datetime as numbers instead of dates - python

I am plotting my Dataframe using Plotly but for some reason, my Datetime values gets converted numbers instead of getting displayed as letters
fig.add_trace(go.Scatter(x=df2plt["PyDate"].values,
y=df2plt["Data"].values))

If df2plt["PyDate"] is already in datetime format:
fig.add_trace(go.Scatter(x=df2plt["PyDate"],
y=df2plt["Data"].values))
Else:
fig.add_trace(go.Scatter(x=pd.to_datetime(df2plt["PyDate"]) ,
y=df2plt["Data"].values))
You can change the display with the variable format:
*format : string, default None
strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds. See strftime documentation for more information on choices: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
*

Related

how to convert "2021-09-01T14:37:40.537Z" into "2021-09-01T14:37:40.5370000+00:00" in python?

How to convert the string "2021-09-01T14:37:40.537Z" into "2021-09-01T14:37:40.5370000+00:00" in python.we have a hive table which is have the datetime in the format of "2021-09-01T14:37:40.537Z" but we want to convert that into "2021-09-01T14:37:40.5370000+00:00" format in python.
General Solution
The datetime module has methods for converting datetimes to and from string representations with arbitrary formats. Specifically, datetime.strptime(date_string, format) converts from string to datetime object, and datetime.strftime(format) converts from datetime to string. By providing different formats to each method, we can convert between them.
from datetime import datetime
inp = "2021-09-01T14:37:40.537Z"
date = datetime.strptime(inp, "%Y-%m-%dT%H:%M:%S.%fZ")
output = date.strftime("%Y-%m-%dT%H:%M:%S.%f0+00:00")
Note that this doesn't take into account timezones. If some entries in your table use time zones other than GMT+00:00, a differnt solution is required.
Alternative solution
In this case, since the two specified formats are so similar, a simpler solution would suffice, although it wouldn't work for other cases. Simply trim the final Z from the input string and append 0000+00:00 as follows:
inp = "2021-09-01T14:37:40.537Z"
output = inp[:-1] + "0000+00:00"

How do I turn string into datetime values and merge

I have a string of yearly data month 1-12, trying to convert it to datetime.month values and then converge it on the main df that already has dt.month values according to some date
usage_12month["MONTH"]= pd.to_datetime(usage_12month["MONTH"])
usage_12month['MONTH'] = usage_12month['MONTH'].dt.month
display(usage_12month)
merge = pd.merge(df,usage_12month, how='left',on='MONTH')
ValueError: Given date string not likely a datetime.
​get the error on the 1st line
.dt.month on a datetime returns an int. So I'm assuming you want to convert usage_12month["MONTH"] from a string to an int to be able to merge it with the other df.
There is a simplier way than converting it to a datetime. You could replace the first two lines by usage_12month["MONTH"]= pd.to_numeric(usage_12month["MONTH"]) and it should work.
--
The error you get on the first line is because you don't specify to the to_datetime function how to interpet the string as a datetime (the number in the string could represent a day, an hour...).
To make your way work you have to give a 'format' parameter to the to_datetime function. In your case, your string contains only the month number, so the format string would be '%m' (see https://strftime.org/) : usage_12month["MONTH"]= pd.to_datetime(usage_12month["MONTH"], format = '%m')
When you're supplying the function with a "usual" date fromat like 'yyyy/mm/dd' it guesses how to interpret it, but it is alway better to provide a format to the function.

Python - Convert epoch in object format to datetime

I'm pulling data from API that gives me date values in epoch in milliseconds, but the values are in this format:
I am trying to convert these to datetime with pd.to_datetime(df1.loc[:, 'Curve Value'], unit='ms'), but am getting the error
ValueError: non convertible value 1,609,459,200,000.0000000000 with
the unit 'ms'
I then tried to format the column into float with df1["Curve Value"] = df["Curve Value"].astype(float), but get then get this error
ValueError: could not convert string to float:
'1,609,459,200,000.0000000000'
I've also tried various ways to remove the commas and convert to float, but get errors trying that, as well.
A bit unwieldy and requires importing datetime from datetime but it works as far as I can see.
df['Real Time'] = df['Curve Value'].apply(lambda t: datetime.fromtimestamp(int(''.join(t.split(',')[:4]))))

How I can write the format of my date when I use apply and datetime functions?

I want to put days first on my datatime format. On other application I've used the following:
df2.Timestamp = pd.to_datetime(df2.Timestamp,dayfirst=True) #the format is d/m/yyyy
Now I want to use apply function, because I have more than one column and instead of doing it in 4 rows I wanted to do it in one row using apply.
df2[["Detection_time", "Device_ack", "Reset/Run", "Duration"]] = df2[["Detection_time", "Device_ack", "Reset/Run", "Duration"]].apply(pd.to_datetime)
But I don't know how to configure "dayfirst" argument.
You can use:
(df2[["Detection_time", "Device_ack", "Reset/Run", "Duration"]]
.apply(pd.to_datetime,dayfirst=True))

Compare two date strings in python (given in specified format in List)

I have two date strings in a list (i.e dateList = ['2013-11-26 08:09:51', '2013-11-26 01:19:51'])
If their is possiblility to compare between date strings of specified format, please provide a solution by returning latest date from the list.
Thanks in Advance
...please provide a solution by returning latest date from the list.
max(dateList)
Because of the formatting of your strings (i.e. starting with the largest time unit and working step by step to the smallest, additional zeroes for single-digit values), they can be directly compared to one another.
You have asked for two different things Compare the dates and get the latest date:
To get the latest date use #jonrsharpe solution:
You can compare them as strings, by using all(), I'm using all so it can work with lots of dates and not just 2:
dateList = ['2013-11-26 08:09:51', '2013-11-26 08:09:51']
if all(dateList[0] == x for x in dateList):
print "Equal"
else:
print "Not equal"
Use dateutil library:
from dateutil import parser
dateList = [parser.parse(date) for date in dateList]
latest_date = max(dateList)
It will give you latest date.
:)

Categories

Resources