Create and initialise a time column Python - python

I need to add a time column to my existing dataframe and initialize it. I tried this line of code df['date']=datetime.time(0, 0, 0) in a small script :
import pandas as pd
import datetime
df = pd.DataFrame({'column1':[34,54,32,23,26]})
df['date']=datetime.time(0, 0, 0)
print(df['date'])
output:
0 00:00:00
1 00:00:00
2 00:00:00
3 00:00:00
4 00:00:00
but when I implemented it in my code, in which I work on large dataframes, I got this error:
dfreez['delta']=datetime.time(0, 0, 0)
TypeError: descriptor 'time' for 'datetime.datetime' objects doesn't apply to 'int' object
this is a piece of my code:
import pandas as pd
dfreez = pd.read_excel('file_name.xlsx',header=0, index= False)
from datetime import datetime
dfreez['delta']=datetime.time(0, 0, 0)
I don't understand what went wrong!

import datetime and from datetime import datetime and not the same.
After the first one, the local datetime variable is a reference to the module. So you can access the datetime class with datetime.datetime and the time class with datetime.time
After the second, the local datetime variable is a reference to the datetime class. So you have no (direct *) way to access the time class.
You should just use:
import datetime
in the second snippet just like what was done in first one.
(*) FYI: it is still possible with the ugly sys.modules['datetime'].time. But never pretend that I advised you to do that!

Related

Difference between pandas datetime and datetime datetime

Hi have some dates in datetime.datetime format that I use to filter a panda dataframe with panda timestamp. I just tried the following and get a 2 hour offset :
from datetime import datetime
import pandas as pd
pd.to_datetime(datetime(2020, 5, 11, 0, 0, 0).timestamp()*1e9)
The output is:
->Timestamp('2020-05-10 22:00:00')
Can anybody explain why this gives a 2 hour offset? I am in Denmark so it corresponds to the offset to GMT. Is this the reason. I can of course just add 2 hours but want to understand why to make the script robust in the future.
Thanks for your help Jesper
pd.to_datetime accepts a datetime object so you could just do (pandas assumes UTC):
pd.to_datetime(datetime(2020, 5, 11))
You are getting a 2 hour offset when converting to a timestamp because by default python's datetime is unaware of timezone and will give you a "naive" datetime object (docs are here: https://docs.python.org/3/library/datetime.html#aware-and-naive-objects). The generated timestamp will be in the local timezone, hence the 2 hour offset.
You can pass in a tzinfo parameter to the datetime object specifying that the time should be treated as UTC:
from datetime import datetime
import pandas as pd
import pytz
pd.to_datetime(datetime(2020, 5, 11, 0, 0, 0, tzinfo=pytz.UTC).timestamp()*1e9)
Alternatively, you can generate a UTC timestamp using the calendar module:
from datetime import datetime
import pandas as pd
import calendar
timestamp = calendar.timegm(datetime(2020, 5, 11, 0, 0, 0).utctimetuple())
pd.to_datetime(timestamp*1e9)
if your datetime objects actually represent local time (i.e. your OS setting), you can simply use
from datetime import datetime
import pandas as pd
t = pd.to_datetime(datetime(2020, 5, 11).astimezone())
# e.g. I'm on CEST, so t is
# Timestamp('2020-05-11 00:00:00+0200', tz='Mitteleuropäische Sommerzeit')
see: How do I get a value of datetime.today() in Python that is “timezone aware”?
Just keep in mind that pandas will treat naive Python datetime objects as if they were UTC:
from datetime import timezone
t1 = pd.to_datetime(datetime(2020, 5, 11, tzinfo=timezone.utc))
t2 = pd.to_datetime(datetime(2020, 5, 11))
t1.timestamp() == t2.timestamp()
# True
see also: Python datetime and pandas give different timestamps for the same date

Can't call strftime on numpy.datetime64, no definition

I have a datetime64 t that I'd like to represent as a string.
When I call strftime like this t.strftime('%Y.%m.%d') I get this error:
AttributeError: 'numpy.datetime64' object has no attribute 'strftime'
What am I missing? I am using Python 3.4.2 and Numpy 1.9.1
Importing a data structures library like pandas to accomplish type conversion feels like overkill to me. You can achieve the same thing with the standard datetime module:
import numpy as np
import datetime
t = np.datetime64('2017-10-26')
t = t.astype(datetime.datetime)
timestring = t.strftime('%Y.%m.%d')
Use this code:
import pandas as pd
t= pd.to_datetime(str(date))
timestring = t.strftime('%Y.%m.%d')
This is the simplest way:
t.item().strftime('%Y.%m.%d')
item() gives you a Python native datetime object, on which all the usual methods are available.
If your goal is only to represent t as a string, the simplest solution is str(t). If you want it in a specific format, you should use one of the solutions above.
One caveat is that np.datetime64 can have different amounts of precision. If t has nanosecond precision, user 12321's solution will still work, but apteryx's and John Zwinck's solutions won't, because t.astype(datetime.datetime) and t.item() return an int:
import numpy as np
print('second precision')
t = np.datetime64('2000-01-01 00:00:00')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
print('microsecond precision')
t = np.datetime64('2000-01-01 00:00:00.0000')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
print('nanosecond precision')
t = np.datetime64('2000-01-01 00:00:00.0000000')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
import pandas as pd
print(pd.to_datetime(str(t)))
second precision
2000-01-01T00:00:00
2000-01-01 00:00:00
2000-01-01 00:00:00
microsecond precision
2000-01-01T00:00:00.000000
2000-01-01 00:00:00
2000-01-01 00:00:00
nanosecond precision
2000-01-01T00:00:00.000000000
946684800000000000
946684800000000000
2000-01-01 00:00:00
For those who might stumble upon this: numpy now has a numpy.datetime_as_string function. Only caveat is that it accepts an array rather than just an individual value. I could make however that this is still a better solution than having to use another library just to do the conversion.
It might help to convert the datetime object to string and use splitting as shown below:
dtObj = 2011-08-01T00:00:00.000000000
dtString = str(dtObj).split('-01T00:00:00.000000000')[0]
print(dtString)
>>> '2011-08-01'

Python - defining global name datetime

I'm trying to create a function where I use various functions from the datetime module, such as strftime, strptime and timedelta.
I feel like I've tried everything, but every time I am told this:
4 date = '2012.09.07'
5
----> 6 q = net(date)
7 print q
/Users/fb/Documents/Python_files/test_function.pyc in net(date)
1 def net(date):
----> 2 b = datetime.strptime(a, '%Y.%m.%d')
3 c = b.strftime('%d:%m:%y')
4 return c
NameError: global name 'datetime' is not defined
I've read that others probably experience the same problem as I, namely ' It works in the python interpreter but not in the script'. Can anyone help, please?
You need to import the datetime object in your module:
from datetime import datetime
at the top of test_function.py.
In your interpreter session you probably already imported the object.
Your whole module will then look like:
from datetime import datetime
def net(date):
b = datetime.strptime(date, '%Y.%m.%d')
c = b.strftime('%d:%m:%y')
return c
where I replaced a with date, since that is the name of the actual argument to the function.
Note that the datetime module contains a datetime class, which is the only thing imported here. If you need access to the date and timedelta classes as well, import these explicitly (from datetime import datetime, date, timedelta) or import just the module and refer to the contents as attributes (import datetime, then datetime.datetime.strptime() and datetime.date.today(), etc.).

type object 'datetime.datetime' has no attribute 'datetime'

I have gotten the following error:
type object 'datetime.datetime' has no attribute 'datetime'
On the following line:
date = datetime.datetime(int(year), int(month), 1)
Does anybody know the reason for the error?
I imported datetime with from datetime import datetime if that helps
Thanks
Datetime is a module that allows for handling of dates, times and datetimes (all of which are datatypes). This means that datetime is both a top-level module as well as being a type within that module. This is confusing.
Your error is probably based on the confusing naming of the module, and what either you or a module you're using has already imported.
>>> import datetime
>>> datetime
<module 'datetime' from '/usr/lib/python2.6/lib-dynload/datetime.so'>
>>> datetime.datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)
But, if you import datetime.datetime:
>>> from datetime import datetime
>>> datetime
<type 'datetime.datetime'>
>>> datetime.datetime(2001,5,1) # You shouldn't expect this to work
# as you imported the type, not the module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)
I suspect you or one of the modules you're using has imported like this:
from datetime import datetime.
For python 3.3
from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)
You should really import the module into its own alias.
import datetime as dt
my_datetime = dt.datetime(year, month, day)
The above has the following benefits over the other solutions:
Calling the variable my_datetime instead of date reduces confusion since there is already a date in the datetime module (datetime.date).
The module and the class (both called datetime) do not shadow each other.
You should use
date = datetime(int(year), int(month), 1)
Or change
from datetime import datetime
to
import datetime
If you have used:
from datetime import datetime
Then simply write the code as:
date = datetime(int(year), int(month), 1)
But if you have used:
import datetime
then only you can write:
date = datetime.datetime(int(2005), int(5), 1)
I run into the same error maybe you have already imported the module by using only import datetime so change from datetime import datetime to only import datetime. It worked for me after I changed it back.
import time
import datetime
from datetime import date,timedelta
You must have imported datetime from datetime.
I found this to be a lot easier
from dateutil import relativedelta
relativedelta.relativedelta(end_time,start_time).seconds
Avoid to write:
from datetime import datetime
datetime.datetime.function()
Solution No. 1:
import datetime
datetime.datetime.function()
Solution No. 2:
from datetime import datetime
datetime.function()
from datetime import datetime
import time
from calendar import timegm
d = datetime.utcnow()
d = d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
utc_time = time.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = timegm(utc_time)
delete one datetime from:
date = datetime.datetime(int(year), int(month), 1)
and you get this:
date = datetime(int(year), int(month), 1)
you already imported the first one with this:
from datetime import datetime
so its redundant.
The Problem Is That You Are Using The Tag
from datetime
I had The Same Problem You Need To use It Like This Instead
import datetime

Check validity of datetime?

I would like to create a setter method for a datetime object so that I can customize the replace method. Is this possible?
For example:
datetime = datetime.replace(day = 34)
This throws an error because 34 is not a valid number of days for any month, but what I would like to do is increase the month by 1 and then subtract 28, 30, or 31 days.
You can subclass datetime and provide your own implementation of the replace method.
this doesn't answer your question directly, but if you're just trying to increment the datetime object, you can use timedelta. For example:
from datetime import datetime
from datetime import timedelta
datetime(2011, 1, 1) + timedelta(days=34)

Categories

Resources