Need Help Converting DateTime Using Python3 - python-3.x

I have the follow code, but having trouble removing the Z at the end. The time format looks like this 2020-07-28T15:47:36.165310258Z
from datetime import datetime
datetime.strptime(f[a]['Created On'], "%Y-%m-%dT%H:%M:%S.%f")
When using the %f, I noticed that it is only able to the next 6 digit. Is there any way to make it take more? This is the error that I am getting (MISSING) ValueError: unconverted data remains: 258Z
What is the best way to remove the Z and have it just return the date?

Was able to get around the issue by just moving the last 4 characters from the string and then using datetime.strptime on it.
datetime.strptime(f[a]['Created On'][:-4], "%Y-%m-%dT%H:%M:%S.%f")

Related

Quantstats - TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime

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

Changing datetime format in py

I have the following code which gives the following output:
print(df1['Diff'].mean())
outputs:
10 days 16:13:29.467455
But since i just want the days value and not the time, i have done this:
print(datetime.strptime(df1['Diff'].mean(), format ='%d')
but i am getting the following error:
^
SyntaxError: unexpected EOF while parsing
Why am i getting this error?
For date, time, and datetime objects
You should be using strftime to format the time, not to parse the time (as in strptime).
print(obj.strftime('%d'))
strptime expects a string to be passed in (and you were passing in a datetime object), whereas strftime formats an existing datetime object.
For timedelta objects
print(obj.days)
This gets the days counterpart you're looking for.
I think the instance of df1['Diff'].mean() is str and datetime.strptime() can be use only in datetime methods. So to only get date you have to take slice of df1['Diff'].mean() like df1['Diff'].mean()[:-14]
Which is in your case.

Python pandas to_datetime works inconsistently: Why are month and day mixed up?

With the python pandas package I run
pd.to_datetime("23.01.2019 06:50:59")
and get the expected result of
Timestamp('2019-01-23 06:50:59')
However, when running
pd.to_datetime("11.01.2019 18:34:39")
day and month are mixed up and I get
Timestamp('2019-11-01 18:34:39')
Expected was: Timestamp('2019-01-11 18:34:39')
Any ideas about why this happens and how to avoid it?
Thanks!
Here is possible use parameter dayfirst=True:
print (pd.to_datetime("11.01.2019 18:34:39", dayfirst=True))
2019-01-11 18:34:39
Generaly solution is with specify format of datetimes:
print (pd.to_datetime("11.01.2019 18:34:39", format='%d.%m.%Y %H:%M:%S'))
2019-01-11 18:34:39
Why are month and day mixed up?
Because pandas try guess format and MMDDYYYY have more priority like DDMMYYY.

How to remove special usual characters from a pandas dataframe using Python

I have a file some crazy stuff in it. It looks like this:
I attempted to get rid of it using this:
df['firstname'] = map(lambda x: x.decode('utf-8','ignore'), df['firstname'])
But I wound up with this in my dataframe: <map object at 0x0000022141F637F0>
I got that example from another question and this seems to be the Python3 method for doing this but I'm not sure what I'm doing wrong.
Edit: For some odd reason someone thinks that this has something to do with getting a map to return a list. The central issue is getting rid of non UTF-8 characters. Whether or not I'm even doing that correctly has yet to be established.
As I understand it, I have to apply an operation to every character in a column of the dataframe. Is there another technique or is map the correct way and if it is, why am I getting the output I've indicated?
Edit2: For some reason, my machine wouldn't let me create an example. I can now. This is what i'm dealing with. All those weird characters need to go.
import pandas as pd
data = [['🦎Ale','Αλέξανδρα'],['��Grain','Girl🌾'],['Đỗ Vũ','ên Anh'],['Don','Johnson']]
df = pd.DataFrame(data,columns=['firstname','lastname'])
print(df)
Edit 3: I tired doing this using a reg ex and for some reason, it still didn't work.
df['firstname'] = df['firstname'].replace('[^a-zA-z\s]',' ')
This regex works FINE in another process, but here, it still leaves the ugly characters.
Edit 4: It turns out that it's image data that we're looking at.

datetime Attribute error in python

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!

Resources