Convert date string to datetime for whole column of df - python-3.x

df = pd.read_csv('bitcoin.csv')
print(df)
gives
Date Open High Low Close Volume
0 Apr 16, 2018 8337.57 8371.15 7925.73 8058.67 5,631,310,000
1 Apr 15, 2018 7999.33 8338.42 7999.33 8329.11 5,244,480,000 ....
I tried
pd.to_datetime(pd.Series(['Date']), format = '%b %d, %Y')
but got
TypeError: Unrecognized value type: && ValueError: time data 'Date' does not match format '%b %d %Y' (match)
I also tried
df['Date'] = df['Date'].apply(lambda x: datetime.datetime.strptime(x, '%b %d, %Y')
but got SyntaxError: unexpected EOF while parsing
when running
print(df['Date'])
after printing it says
Name: Date, Length: 1567, dtype: object
Not sure whats going on here? Is it already a datetime object?

Seems like you're missing an extra parenthesis at the end here:
df['Date'] = df['Date'].apply(lambda x: datetime.datetime.strptime(x, '%b %d, %Y'))
I would suggest you do something like this, adding to #COLDSPEED comment:
df['Date'] = df['Date'].apply(lambda x: pd.to_datetime(x, format = '%b %d, %Y', errors = 'coerce'))

df['Date'] = pd.to_datetime(df.Date).dt.strftime('%b %d, %Y')
Output
0 Apr 16, Jan 01, 1970
1 Apr 15, Jan 01, 1970
Name: Date, dtype: object

Related

Convert paypal date to string in python

paypal is giving me this format '03:00:00 Mar 14, 2023 PDT'. I've tried different solutions but i can't get with the right one. How can i separate each item into a string using python?
Assuming you want a datetime object
from datetime import datetime
time_string = "03:00:00 Mar 14, 2023 PDT"
d = datetime.strptime(time_string[:-4], "%H:%M:%S %b %d, %Y")
or
import parsedatetime as pdt # $ pip3 install parsedatetime
cal = pdt.Calendar()
time_string = "03:00:00 Mar 14, 2023 PDT"
d = cal.parseDT(time_string)[0]
Once you have your datetime, output the part(s) you want using strftime

Why does the strptime function not interpret the date_time_str2 case here?

I've reviewed this many times and don't understand why the str2 case won't convert correct? This is being run in Python 3.8.
from datetime import datetime
import time
print(time.tzname)
date_time_str1 = '(2021,10,4) (19,36,21)'
date_time_str2 = 'Mon Oct 4 11:13:08 2021'
print(date_time_str1)
print(date_time_str2)
date_time_obj = datetime.strptime(date_time_str1, '(%Y,%m,%d) (%H,%M,%S)')
print ("The date is", date_time_obj)
date_time_obj = datetime.strptime(date_time_str2, '%A %B %d %H:%M:%S %Y')
print ("The date is", date_time_obj)
You should use '%a %b %d %H:%M:%S %Y' because your day and month names are abbreviated.

How do i make Django accept my date formats

How can i make my model date field accept the 'dd/mm/yyyy' date format?
I have seen the solutions using django forms but i am not using django forms.
Please help.
Django settings.py has a date input area, the defaults are here. https://docs.djangoproject.com/en/3.1/ref/settings/#date-input-formats
[
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
'%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
'%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006'
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
]
However, if one needs more control, python's strptime() module does this
import datetime
date_time_str = '16-12-2020 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%d-%m-%Y %H:%M:%S.%f')
If you are using bootstrap datepicker as i used in my form, you can simply change the date format in the datepicker.js file or alternatively add the attribute data-date-format="yyyy-mm-dd".

Syntax Error <expression Line 1>

I'm trying to convert a date in a string format (13/06/2017) into a date format June 13, 2007. I have written the code but I keep getting a syntax error for my first line which is the definition of the function line.
My code is this:
def printDate(date):
import datetime
newdate = datetime.strptime(date, %d/%m/%Y)
d = newdate.strftime(%b %d, %Y)
return d
You didn't pass the parameter "date format" as a string, that's why, also be sure to import datetime module as follows:
from datetime import datetime
def printDate(date):
newdate = datetime.strptime(date, "%d/%m/%Y")
d = newdate.strftime("%B %d, %Y")
return d
Test:
printDate("13/06/2017")
>> 'June 13, 2017'

Why is there a difference between datetime.utcnow() and datetime.now(timezone('UTC'))?

As you can see from the snippet below, there's a one hour difference between the two method. What is the reason ?
from datetime import datetime
from pytz import timezone
import time
def timestamp2date(timestamp):
# function converts a UTC timestamp into Europe/Zurich Gregorian date
DATE_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
utcTimeStamp = datetime.fromtimestamp(int(timestamp)).replace(tzinfo=timezone('UTC'))
return utcTimeStamp.astimezone(timezone('Europe/Zurich')).strftime(DATE_TIME_FORMAT)
timeStampUTC_1 = time.mktime(datetime.utcnow().timetuple())
print(timeStampUTC_1)
print(timestamp2date(timeStampUTC_1))
timeStampUTC_2 = time.mktime(datetime.now(timezone('UTC')).timetuple())
print(timeStampUTC_2)
print(timestamp2date(timeStampUTC_2))
print(timeStampUTC_2 - timeStampUTC_1)
# 1504385450.0
# 2017-09-03 00:50:50 this the right time
# 1504389050.0
# 2017-09-03 01:50:50
# 3600.0
The reason is that datetime.timetuple() sets dst=-1 if the datetime is not offset aware and dst=0 or 1 when it is offset aware.
From the documentation:
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
In []:
datetime.utcnow()
Out[]:
datetime.datetime(2017, 9, 2, 23, 9, 12, 715042)
In []:
print(datetime.utcnow().dst())
Out[]:
None
In []:
datetime.now(timezone('UTC'))
Out[]:
datetime.datetime(2017, 9, 2, 23, 9, 15, 856983, tzinfo=<UTC>)
In []:
datetime.now(timezone('UTC')).dst()
Out[]
datetime.timedelta(0)
In []:
datetime(2017, 9, 2, 23, 9, 15, 856983).timetuple()
Out[]:
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=2, tm_hour=23, tm_min=9,
tm_sec=15, tm_wday=5, tm_yday=245, tm_isdst=-1)
^^
In []:
datetime(2017, 9, 2, 23, 9, 15, 856983, timezone('UTC')).timetuple()
Out[]:
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=2, tm_hour=23, tm_min=9,
tm_sec=15, tm_wday=5, tm_yday=245, tm_isdst=0)
^
This changes the timestamp that time.mktime() generates because mktime treats -1 unknown and uses localtime, so may calculate dst=1 and hence they could be an 1 hour different (3600s):
In []:
time.mktime(datetime(2017, 9, 2, 23, 9, 15, 856983).timetuple())
Out[]:
1504411755.0
In []:
time.mktime(datetime(2017, 9, 2, 23, 9, 15, 856983, timezone('UTC')).timetuple())
Out[]:
1504415355.0
Here's the result of my further exploration of the timezone concept associated with the daylight saving time flag. The code is pretty straightforward.
To summarize the results, there's a right way to handle date/time information:
Date/time object must be localized
If localized correctly, the date/time object shows right daylight saving time information
This is reflected when you convert your localized date/time object to a UTC date/time object which is by definition DST agnostic
All those considerations are particularly relevant when, for example, you access a stock or currency historical minute rate API
code:
from datetime import datetime
from pytz import timezone
import time
print("---- Winter time (CET=Central European Time) ----")
dateStr = "2014-02-28 22:28:15"
datetimeObjUnlocalized = datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S")
print('UNL: ' + datetimeObjUnlocalized.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print(' datetimeObjUnlocalized-->tm_isdst=' + str(datetimeObjUnlocalized.timetuple()[8]))
datetimeObjZH = timezone('Europe/Zurich').localize(datetimeObjUnlocalized)
print('ZH: ' + datetimeObjZH.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print(' datetimeObjZH-->tm_isdst=' + str(datetimeObjZH.timetuple()[8]))
print("UTC: " + datetimeObjZH.astimezone(timezone('UTC')).strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print("\n---- Summer time (CEST=Central European Summer Time) ----")
dateStr = "2014-06-28 22:28:15"
datetimeObjUnlocalized = datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S")
print('UNL: ' + datetimeObjUnlocalized.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print(' datetimeObjUnlocalized-->tm_isdst=' + str(datetimeObjUnlocalized.timetuple()[8]))
datetimeObjZH = timezone('Europe/Zurich').localize(datetimeObjUnlocalized)
print('ZH: ' + datetimeObjZH.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print(' datetimeObjZH-->tm_isdst=' + str(datetimeObjZH.timetuple()[8]))
print("UTC: " + datetimeObjZH.astimezone(timezone('UTC')).strftime("%Y-%m-%d %H:%M:%S %Z%z"))

Resources