I have some timestamps in python pandas, Timestamp('2000-02-09 00:00:00') and I would like to convert them to Timestamp('2000-02-09 13:00:00'). Just adding 13 hours wouldn't work as some of them have different time. Can you point to a solution to this problem?
Use replace method of pandas timestamp objects:
import pandas as pd
t = pd.Timestamp('2000-02-09 00:00:00')
t = t.replace(hour=13, minute=0, second=0)
pandas.Timestamp is a datetime subclass and therefore it has all its methods such as .replace():
>>> import pandas as pd
>>> from datetime import datetime
>>> issubclass(pd.Timestamp, datetime)
True
>>> isinstance(pd.Timestamp('2000-02-09 00:00:00'), datetime)
True
>>> pd.Timestamp('2000-02-09 00:00:00').replace(hour=13)
Timestamp('2000-02-09 13:00:00')
Related
I have a dataframe that looks like that:
conversation__created_at
0 2020-10-15T03:39:42.766773+00:00
1 2020-10-14T11:24:33.831177+00:00
2 2020-10-14T08:29:44.192258+00:00
3 2020-10-14T01:42:06.674313+00:00
4 2020-10-13T12:57:04.218184+00:00
How to convert it into GMT +7?
I assume you have a pandas series because the data you posted looks like one.
Then you can use tz_convert, i.e.
import pandas as pd
pd.to_datetime('2020-10-15T03:39:42.766773+00:00').tz_convert('Etc/GMT+7')
As pointed out in the comments, since the datetime carries a T in it, it is of string format, thus we need to convert to datetime first and then convert to the correct timezone.
pd.to_datetime(series).dt.tz_convert('Etc/GMT+7')
You can use datetime library only.
from datetime import datetime, timedelta, timezone
d = datetime.fromisoformat("2020-10-15T03:39:42.766773+00:00")
tz = timezone(timedelta(hours=7))
new_time = d.astimezone(tz)
you can use pytz to set timezone for your datetime instance
for example:
from pytz import timezone
from datetime import datetime
date = datetime.now()
print(date)
tz = timezone("Etc/GMT+7")
date = date.replace(tzinfo=tz)
print(date)
out put:
2020-10-26 10:33:25.934699
2020-10-26 10:33:25.934699-07:00
You can apply pytz.timezone on the df
from pytz import timezone
from datetime import datetime
def myDate(x):
tz = timezone("Etc/GMT+7")
dt = x.replace(tzinfo=tz)
return dt
df['conversation__created_at'] = df.apply(lambda row: myDate(row['conversation__created_at'].to_pydatetime()))
I am using Python in dynamo and I am facing a problem.
I have to convert date time into integer so I could further process
it
I have tried some codes but they are not helpful.
If you'd like to convert the datetime to a unix timestamp (number of seconds elapsed since Jan 1, 1970), then you can do
>>> import datetime as dt
>>> ts = dt.datetime.now()
>>> print(int(ts.timestamp())
1588967243
Maybe you want to get timestamp?
import time
import datetime
s = "01/12/2011"
time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
Result: 1322697600.0
I am trying to create timezone aware date column in a pandas DataFrame. When I run the code below, the resulting pandas column does not have the same datetime as the one I inputted. What am I doing wrong here?
I am using python 3.6.2 and pandas 0.20.3
from datetime import datetime
import pandas as pd
import pytz
date_string = "12/14/2016 12:00"
timezone = pytz.timezone("US/Pacific")
input_datetime = datetime.strptime(date_string, "%m/%d/%Y %H:%M").replace(tzinfo=timezone)
df = pd.DataFrame({"datetime":[input_datetime]})
If I run that code, df['datetime'][0].minute returns 53 while input_datetime.minute returns 0.
When I don't replace the tzinfo I do not have a problem.
If you first convert your input_datetime you can call the minutes (or years etc) of your dataframe with .dt.minute
input_datetime = pd.to_datetime(datetime.strptime(date_string,
"%m/%d/%Y %H:%M")).replace(tzinfo=timezone)
df = pd.DataFrame({"datetime":[input_datetime]})
df['datetime'].dt.minute
You can use pandas .dt and tz_localize:
from datetime import datetime
import pandas as pd
date_string = "12/14/2016 12:00"
df = pd.DataFrame({'datetime':[datetime.strptime(date_string, "%m/%d/%Y %H:%M")]})
df['datetime'].dt.tz_localize('US/Pacific')
Output:
0 2016-12-14 12:00:00-08:00
Name: datetime, dtype: datetime64[ns, US/Pacific]
I have a date timestamp like "2013-12-20 23:40:33". Now, my requirement is to re-format this date in reverse order like :
<seconds><minutes><hr><day><month><year>
in python. Please suggest
Load the string into a datetime object with strptime and format to string with strftime:
>>> from datetime import datetime
>>> datetime.strptime(s, '%Y-%m-%d %H:%M:%S').strftime('%S:%M:%H %d-%m-%Y')
'33:40:23 20-12-2013'
>>> import datetime
>>> datetime.datetime.strptime('2013-12-20 23:40:33', '%Y-%m-%d %H:%M:%S').strftime('%S:%M:%H %d-%m-%Y')
'33:40:23 20-12-2013'
If you don't need to validate the time string:
>>> import re
>>> '<%s>' % '><'.join(re.findall(r'\d+', "2013-12-20 23:40:33")[::-1])
'<33><40><23><20><12><2013>'
It is 6 times faster than the corresponding datetime solution:
>>> from datetime import datetime
>>> datetime.strptime("2013-12-20 23:40:33", '%Y-%m-%d %H:%M:%S').strftime('<%S><%M><%H><%d><%m><%Y>')
'<33><40><23><20><12><2013>'
Or 5 times faster than time solution:
>>> import time
>>> time.strftime('<%S><%M><%H><%d><%m><%Y>', time.strptime("2013-12-20 23:40:33", '%Y-%m-%d %H:%M:%S'))
'<33><40><23><20><12><2013>'
d = "2013-12-20 23:40:33"
date = d[17]+d[18]+":"+d[14]+d[15]+":"+d[11]+d[12]+" "+d[8]+d[9]+"-"+d[5]+d[6]+"-"+d[0]+d[1]+d[2]+d[3]
print(d)
print(date)
I'm pulling a timestamp that looks like this - 2014-02-03T19:24:07Z
I'm trying to calculate the number of days since January 1.
I was able to convert it to datetime using
yourdate = dateutil.parser.parse(timestamp)
But now I'm trying to parse it and grab individual elements, such as the month & day.
Is there a way to convert it to strptime so I can select each element?
Just access the month, day using year, month, day attributes..
>>> import dateutil.parser
>>> yourdate = dateutil.parser.parse('2014-02-03T19:24:07Z')
>>> yourdate.year
2014
>>> yourdate.month
2
>>> yourdate.day
3
Just to be a little more complete:
>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> import pytz
>>> d = parse('2014-02-03T19:24:07Z')
>>> other = datetime(year=2014, month=1, day=1, tzinfo=pytz.utc)
>>> (d-other).days
33
You have to make sure the other datetime is timezone aware if you're creating it with datetime as opposed to the datetime you're parsing with dateutil.
There's no need for converting. The resulting datetime.datetime object has all necessary properties which you can access directly. For example:
>>> import dateutil.parser
>>> timestamp="2014-02-03T19:24:07Z"
>>> yourdate = dateutil.parser.parse(timestamp)
>>> yourdate.day
3
>>> yourdate.month
2
See: https://docs.python.org/2/library/datetime.html#datetime-objects
if you want to calculate:
import dateutil.parser
yourdate = dateutil.parser.parse('2014-02-03T19:24:07Z')
startdate = dateutil.parser.parse('2014-01-01T00:00:00Z')
print (yourdate - startdate)
Another way to solve without the dateutil module:
import datetime
# start date for comparision
start = datetime.date(2014, 1, 1)
# timestamp as string
datefmt = "%Y-%m-%dT%H:%M:%SZ"
current = "2014-02-03T19:24:07Z"
# convert timestamp string to date, dropping time
end = datetime.datetime.strptime(current, datefmt).date()
# compare dates and get number of days from timedelta object
days = (end - start).days
This assumes you don't care about time (including timezones).