Few questions regarding chapter "Working with dates and times in python"

Screen Link:

My Code:

Hi everyone,
There are few aspects from the chapter “Working with Dates and Times in Python” that are not entirely clear to me, so I was wondering if someone could help me out to clarify them:

-In order to call the strptime constructor, we need to write the following code:

date_1_str = “24/12/1984”
date_1_dt = dt.datetime.strptime(date_1_str, “%d/%m/%Y”)
print(type(date_1_dt))
print(date_1_dt)

That means for date_1_dt we need to write: module(dt).datetime(class).strptime(constructor)
My question is the following: does this also apply for the strftime constructor? Would the coding be like this?:
module(dt).datetime(class).strftime(constructor)

-When using the strptime constructor to parse string as dates (mission 5), the coding is as follows:

for row in potus:
appt_start_date_str = str(row[2])
appt_start_date = dt.datetime.strptime(appt_start_date_str, date_format)
row[2] = appt_start_date

In this case, after changing the variable apt_start_date_str from a string to a datetime format, we need to reassign this value to row[2] (last line of code).

However, when using the strftime constructor to format datetime objects to strings (mission 6), the coding is as follows:

for row in potus:
app_start_date = row[2]
app_start_date_str = app_start_date.strftime("%B, %Y")
if app_start_date_str not in visitors_per_month:
visitors_per_month[app_start_date_str] = 1
else:
visitors_per_month[app_start_date_str] += 1

In this case, we do not need to reassign the value of app_start_date_str back to row[2] (there is no line with row[2] = app_start_date_str in the code). Could please someone explain to me why reassigning the value back to row[2] is needed and why only for one of the cases?

Many thanks in advance :slight_smile:
Replace this line with your code


What I expected to happen:


What actually happened: 

Replace this line with the output/error


<!--Enter other details below: -->

hello @aitor.susperregui,
Have a look here:

  • strptime - the p means parse, we parse a string to a datetime object, it is a class (datetime) method. strptime(date_string, format)
  • strftime - the f means format, we convert a datetime object into a string, it is an instance (datetime object) method. strftime(format)
1 Like

To understand this fully I’ll provide examples:

  1. strptime, we will parse a string to a date time object.
    • Let’s import datetime from datetime: there’s no need of importing the entire datetime module, since we want to interact with the class method, strptime, we’ll only import the datetime class
from datetime import datetime

Now we’ll create a date string, and it’s accompanying format.

date_string = '2020/10/7'
strp_format = '%Y/%m/%d'

We’ll use datetime.strptime to create a datetime object.

>>>date = datetime.strptime(date_string, strp_format)
>>>date

datetime.datetime(2020, 10, 7, 0, 0)
  1. date is a datetime object, we can use strftime which is an instance method to access anything we want from date. For teaching purposes well use %c in order to get Locale’s appropriate date and time representation.
>>>date.strftime('%c')

'Wed Oct  7 00:00:00 2020'

And that’s how we converted a datetime object to get the Locale’s appropriate time.


You can use dir() function to see attributes or methods of date variable.

Expand to see the date variable attributes and methods
>>>dir(date)

['__add__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__radd__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rsub__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 'astimezone',
 'combine',
 'ctime',
 'date',
 'day',
 'dst',
 'fold',
 'fromordinal',
 'fromtimestamp',
 'hour',
 'isocalendar',
 'isoformat',
 'isoweekday',
 'max',
 'microsecond',
 'min',
 'minute',
 'month',
 'now',
 'replace',
 'resolution',
 'second',
 'strftime',
 'strptime',
 'time',
 'timestamp',
 'timetuple',
 'timetz',
 'today',
 'toordinal',
 'tzinfo',
 'tzname',
 'utcfromtimestamp',
 'utcnow',
 'utcoffset',
 'utctimetuple',
 'weekday',
 'year']
2 Likes

Hi @info.victoromondi

Thanks a lot for your answer! It really helped me to clarify my first point. Now, could you help me clarifying my second question? (it involves the line of code row[2] = appt_start_date):

-When using the strptime constructor to parse string as dates (mission 5), the coding is as follows:

for row in potus:
appt_start_date_str = str(row[2])
appt_start_date = dt.datetime.strptime(appt_start_date_str, date_format)
row[2] = appt_start_date

In this case, after changing the variable apt_start_date_str from a string to a datetime format, we need to reassign this value to row[2] (last line of code).

However, when using the strftime constructor to format datetime objects to strings (mission 6), the coding is as follows:

for row in potus:
app_start_date = row[2]
app_start_date_str = app_start_date.strftime("%B, %Y")

if app_start_date_str not in visitors_per_month:
visitors_per_month[app_start_date_str] = 1
else:
visitors_per_month[app_start_date_str] += 1

In this case, we do not need to reassign the value of app_start_date_str back to row[2] (there is no line with row[2] = app_start_date_str in the code). Could please someone explain to me why reassigning the value back to row[2] is needed and why only for one of the cases?

1 Like

@aitor.susperregui

Here you use strftime() to create a string from the datetime object. You need this string to be the key for your dictionary, visitors_per_month. You only need two part of the datetime object for the key: %B, %Y.

In the first case, you are converting a string to a datetime object with strptime().

You reassigned the value back for the first case so that all the date string will now be datetime objects. In the second case, you use convert this datetime object to a string, you want to count the number of visitors at this particular date. You use this string date in your dictionary.

2 Likes

Thanks a lot for your answer @monorienaghogho, now I think I get it!

2 Likes

@aitor.susperregui: please remember to adhere to these guidelines in future when asking questions.

2 Likes