Excuse my lack of knowledge playing with time in python3 but I am stuck for 30 minutes already in this problem so any help will be highly appreciated.
I have salesforce time coming in my script as 2016-11-15T23:49:48.000Z. How can I convert it into mysql datetime format without storing it in a variable.
Desired output format: 2016-11-16 12:11:21.525885
You can use the time module's strptime referenced here.
I'm making some assumptions about the input data, but here's a format that will probably work:
import time
import datetime
def translate_timestamp(timestamp):
t = time.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
s = datetime.datetime.fromtimestamp(time.mktime(t))
return s.strftime("%Y-%m-%d %H:%M:%S.%f")
An example might be:
>>> translate_timestamp("2016-11-15T23:49:48.000Z")
'2016-11-15 23:49:48.000000'
Related
I'm trying to use some of the Quantstats modules, specifically the quantstats.reports module, in Anaconda to get some metrics reports on a portfolio I've designed. I'm fairly new to Python/Quantstats and am really just trying to get a feel for the library.
I've written the following code to utilize the report module to spit out a complete html report and save it under the Output folder:
import quantstats as qs
qs.extend_pandas()
stock = qs.utils.download_returns('GLD')
qs.reports.html(stock, output='Output/GLD.html')
I then get the following TypeError:
TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime
I believe this may be a result of the datetime64 class being localized to my timezone and datetime remaining TZ naive. Frankly, digging through the Quantstats code has been a little beyond my current skillset.
If anybody has any recommendations for fixes, I would greatly appreciate it.
I came upon this while DDGing exactly the same issue.
Not sure which of your columns has the timezone localisation in it, but
df['date'] = df['date'].dt.tz_localize(None)
will get rid of localization for the column df['date']
Incidentally, the usual situation is that the index of a pandas timeseries contains np.Datetime64 types, but when you assign it to a column via
df['date'] = df.index
the resulting column contains pandas Timestamps.
I got this issue resolved after I had lowered the yfinance version from latest version 0.1.87 to
yfinance => 0.1.74
I am given the following object:
datetime.time(18, 50)
I want to convert to a string:
"18:50:00"
Please advise how can I do this?
Tried strftime but getting errors.
datetime.time(18, 50).strftime("%H:%M:%S")
You need to do:
from datetime import time
print(time(18, 50).strftime("%H:%M:%S"))
Importing the time function from datetime module seems to be different from datetime instance's time method.
I am using python3 and I have a time data format like '01-FEB-17' and I need to parse it with datetime.strptime. Does anyone know which format is the correct for this? I tried '%d-%b-%Y', '%d-%m-%Y',... and none of them worked.
Thanks
You can use this
from datetime import datetime
t = datetime.strptime('01-FEB-17', '%d-%b-%y')
NOTE: You can head here for a full list of format specifiers to use
I need your help.
I have parsed a web site, and I have harvested this:
2018-08-18T23:31:00
I searched, but couldn't find how to change datetime to timestamp
Example of my desired result :
1535414693077
The previous answer is incorrect as there is no '%s' formatter in datetime, even though it works, sometimes. See this answer for how wrong it is to use '%s'.
from datetime import datetime
import pytz
pytz.utc.localize(datetime.strptime('2018-08-18T23:31:00', '%Y-%m-%dT%H:%M:%S')).timestamp()
Output (float): 1534635060.0
I'm trying to run to following three lines of python code on command line using Python 3.5.0. It gives me an error- Attribute error: module 'datetime' has no attribute 'date'. I just want to print current date. Please help.
import datetime
current = datetime.date.today()
print(current)
There is nothing wrong with your code. It could be reduced a bit though:
import datetime
datetime.date
which should also cause the error. If this really causes the error, I would say your installation is messed up or, unlikely, there's a bug in Python. Please also make sure you don't have a datetime.py in your working directory. Further, check the output of dir(datetime) after importing it and with a different version of Python.
You shouldn't be getting any error running the above code as there is nothing wrong with it. Also rather than using the above code (which is okay syntax-wise but imports all the names accessible in the datetime moudule), you could use
from datetime import date
current = date.today()
print(current)
since all you want to import is the day's date.
when i run it on python 27. the code returns date with no errors!