How save datetime objects in list (python, csv) - python

Can anyone please tell me how to save my parsed datetime objects to a list? Please see code after the last comment where the problem comes up - Why do I get the AttributeError: 'datetime.datetime' object has no attribute 'toList'? Thanks!
from datetime import datetime, timedelta
import pandas as pd
from dateutil.parser import parse
csvFile = pd.read_csv('myFile.csv')
column = csvFile['timestamp']
column = column.str.slice(0, 19, 1)
dt1 = datetime.strptime(column[1], '%Y-%m-%d %H:%M:%S')
print("dt1", dt1) #output: dt1 2010-12-30 15:06:00
dt2 = datetime.strptime(column[2], '%Y-%m-%d %H:%M:%S')
print("dt2", dt2) #output: dt2 2010-12-30 16:34:00
dt3 = dt1 - dt2
print("dt3", dt3) #output: dt3 -1 day, 22:32:00
#works:
for row in range(len(column)):
timestamp = datetime.strptime(column[row], '%Y-%m-%d %H:%M:%S')
print("timestamp", timestamp) #output (excerpt): timestamp 2010-12-30 14:32:00 timestamp 2010-12-30 15:06:00
#trying to save all parsed timestamps in list, NOT WORKING
myNewList = timestamp.toList()
print(myNewList)

you should create the list before the for loop, and then add each element to it in the loop, like so:
myNewList = []
#works:
for row in range(len(column)):
timestamp = datetime.strptime(column[row], '%Y-%m-%d %H:%M:%S')
print("timestamp", timestamp)
myNewList.append(timestamp)
print(myNewList)

Related

How To Format Date and Time in Python Using Pandas

I need a way to reformat the date and time from 2021-01-27T12:00:17Z as a separate date and time variable in the format as shown below:
Date: 27/01/2021
Time: 12:00
import pandas as pd
values = {'dates': ['2021-01-27T12:00:17Z']}
df = pd.DataFrame(values)
df['dates'] = pd.to_datetime(df['dates'], format='%Y-%m-%dT%H:%M:%SZ')
formatted_date = pd.to_datetime(df['dates']).dt.date
print('Formatted Date:',formatted_date)
formatted_time = pd.to_datetime(df['dates']).dt.time
print('Formatted Time:',formatted_time)
print ('df value:', df)
print (df.dtypes)
When I change the syntax from format='%Y-%m-%dT%H:%M:%SZ' to format='%d-%m-%YT%H:%M:%SZ' it produces an error.
Any help would be much appreciated.
I am using these, hope it helps;
from datetime import datetime, timedelta, timezone
utc_time = datetime.fromtimestamp(date_time).astimezone(timezone.utc)
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz)
date = datetime.fromisoformat(date_time).astimezone(local_tz).date
time = datetime.fromisoformat(date_time).astimezone(local_tz).time
for datetime calculation, we can use timedelta
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz) + deltatime(hours=5)
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz) + deltatime(minutes=60)
<built-in method date of datetime.datetime object at 0x000002BF40795DA0>
<built-in method time of datetime.datetime object at 0x000002BF40795DA0>
the date and time are datetime.datetime objects.

How to fix date formatting using python3

I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.

How to convert timestamp into string in Python

I have a problem with the following code. I get an error "strptime() argument 1 must be str, not Timestamp"
I guess that what I should do is to convert date from timestamp to string but I do not know what to do.
class TweetAnalyzer:
def tweets_to_data_frame(self,ElonMuskTweets):
df = pd.DataFrame(data=[tweet.text for tweet in ElonMuskTweets],columns=['Tweets'])
df['Text length'] = np.array ([len(tweet.text)for tweet in ElonMuskTweets])
df['Date and time of creation'] = np.array ([tweet.created_at for tweet in ElonMuskTweets])
df['Likes'] = np.array ([tweet.favorite_count for tweet in ElonMuskTweets])
df['Retweets'] = np.array ([tweet.retweet_count for tweet in ElonMuskTweets])
list_of_dates = []
list_of_times = []
for date in df['Date and time of creation']:
date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
list_of_dates.append(date_time_obj.date())
list_of_times.append(date_time_obj.time())
df['Date'] = list_of_dates
df['Time'] = list_of_times
df['Date'] = pd.to_datetime(df['Date'])
start_date = '2018-04-13'
end_date = '2019-04-13'
mask1 = (df['Date'] >= start_date) & (df['Date'] <= end_date)
MuskTweets18_19 = df.loc[mask1]
return MuskTweets18_19.to_csv ('elonmusk_tweets.csv',index=False)
I get the error in
date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
How can I solve this prolem?
Thank you in advance
Can you coerce the data type to a string to perform this calculation?
date_time_obj = datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S')
If it says "strptime() argument 1 must be str, not Timestamp", likely that you already have the pandas.Timestamp object, i.e., it is not a string but a parsed date time, only it is in Pandas' format, not Python's. So to convert, use this:
date_time_obj = date.to_pydatetime()
instead of date_time_obj = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
If the object is a Python Timestamp, you can implement:
timestamp = Timestamp('2017-11-12 00:00:00')
str_timestamp = str(timestamp)
import pandas as pd
import datetime
base = pd.to_datetime("2022-10-10")
date_list = [datetime.datetime.strftime(pd.to_datetime(base - datetime.timedelta(days=x)),"%Y-%m-%d") for x in range(7)]
print(date_list)
output will be
['2022-10-10',
'2022-10-09',
'2022-10-08',
'2022-10-07',
'2022-10-06',
'2022-10-05',
'2022-10-04']
Just adding to the above answers as ran into the following probem using the solutions provided:
AttributeError: module 'datetime' has no attribute 'strptime'
Based on the answer found here, you need to either coerce the timestamp into a string like this:
date_time_obj = datetime.datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S')
Or make sure to import the class and not just the module like this:
from datetime import datetime

Parse timestamp as date time and perform function

I am unexperienced with Python and am trying to parse all timestamps of the following csv as datetime objects in order to then perform functions on them (e.g. find timestamp differences etc.).
However, I can parse single lines but not the whole timestamp column. I am getting a 'KeyError: '2010-12-30 14:32:00' for the first date of the timestamp column, when reaching the line below my 'not working' comment.
Thanks in advance.
from datetime import datetime, timedelta
import pandas as pd
from dateutil.parser import parse
csvFile = pd.read_csv('runningComplete.csv')
column = csvFile['timestamp']
column = column.str.slice(0, 19, 1)
print(column)
dt1 = datetime.strptime(column[1], '%Y-%m-%d %H:%M:%S')
print(dt1)
dt2 = datetime.strptime(column[2], '%Y-%m-%d %H:%M:%S')
print(dt1)
dt3 = dt1 - dt2
print(dt3)
for row in column:
print(row)
Not working:
for row in column:
timestamp = datetime.strptime(column[row], '%Y-%m-%d %H:%M:%S')

How to convert a timedelta object into a datetime object

What is the proper way to convert a timedelta object into a datetime object?
I immediately think of something like datetime(0)+deltaObj, but that's not very nice... Isn't there a toDateTime() function or something of the sort?
It doesn't make sense to convert a timedelta into a datetime, but it does make sense to pick an initial or starting datetime and add or subtract a timedelta from that.
>>> import datetime
>>> today = datetime.datetime.today()
>>> today
datetime.datetime(2010, 3, 9, 18, 25, 19, 474362)
>>> today + datetime.timedelta(days=1)
datetime.datetime(2010, 3, 10, 18, 25, 19, 474362)
Since a datetime represents a time within a single day, your timedelta should be less than 24 hours (86400 seconds), even though timedeltas are not subject to this constraint.
import datetime
seconds = 86399
td = datetime.timedelta(seconds=seconds)
print(td)
dt = datetime.datetime.strptime(str(td), "%H:%M:%S")
print(dt)
23:59:59
1900-01-01 23:59:59
If you don't want a default date and know the date of your timedelta:
date = "05/15/2020"
dt2 = datetime.datetime.strptime("{} {}".format(date, td), "%m/%d/%Y %H:%M:%S")
print(dt2)
2020-05-15 23:59:59
I found that I could take the .total_seconds() and use that to create a new time object (or datetime object if needed).
import time
import datetime
start_dt_obj = datetime.datetime.fromtimestamp(start_timestamp)
stop_dt_obj = datetime.datetime.fromtimestamp(stop_timestamp)
delta = stop_dt_obj - start_dt_obj
delta_as_time_obj = time.gmtime(delta.total_seconds())
This allows you to do something like:
print('The duration was {0}'.format(
time.strftime('%H:%M', delta_as_time_obj)
)
Improving #sadpanduar answer with example on converting one column in pandas.DataFrame:
from datetime import timedelta
import time
def seconds_to_datetime(seconds, format='%Y-%m-%d %H:%M:%S'):
td = timedelta(seconds=seconds)
time_obj = time.gmtime(td.total_seconds())
return time.strftime(format, time_obj)
df = pd.read_csv(CSV_PATH)
df['TIMESTAMP_COLUMN'] = df['TIMESTAMP_COLUMN'].apply(seconds_to_datetime)
import datetime`enter code here
lastDownloadedDate = datetime.date(2022,8,4)
print('lastDownloadedDate: ', lastDownloadedDate)
fdate = lastDownloadedDate + datetime.timedelta(days=1)
fdate = datetime.datetime.strptime(str(fdate), "%Y-%m-%d")
fdate = datetime.date(fdate.year, fdate.month, fdate.day)
print('fdate: ', dt3)`

Categories

Resources