AttributeError: 'module' object has no attribute 'strptime'

The error I’m getting seems to imply that the datetime module loaded in the DQ interface doesn’t have strptime - is that correct?

Screen Link: Learn data science with Python and R projects

My Code:

import datetime as dt

def subs_in_month(ym):
    count = 0
    ym = str(ym)
    ym = dt.strptime(ym,"%Y%m")
    start = ym.replace(day=1)
    end = start - dt.timedelta(days=1)
    if row['start_date'] < start and row['end_date'] > end:
        count += 1
    return count

churn['total_customers'] = churn['yearmonth'].apply(subs_in_month)

What I expected to happen: I’m expecting the new column ‘total_customers’ to populate with a count of customers that satisfy the conditions in row 10. I’m working iteratively so the function isn’t finished - the references to “row” and the return output are works in progress.

What actually happened:

AttributeErrorTraceback (most recent call last)
<ipython-input-1-10ae59a8b63d> in <module>()
     12     return count
---> 14 churn['total_customers'] = churn['yearmonth'].apply(subs_in_month)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in apply(self, func, convert_dtype, args, **kwds)
   2549             else:
   2550                 values = self.asobject
-> 2551                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2553         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-1-10ae59a8b63d> in subs_in_month(ym)
      5     ym = str(ym)
      6     print(type(ym))
----> 7     ym = dt.strptime(ym,"%Y%m")
      8     start = ym.replace(day=1)
      9     end = start - dt.timedelta(days=1)

AttributeError: 'module' object has no attribute 'strptime'

Technically that’s correct because datetime module does not have strptime(). It’s not a DQ platform issue, it’s the module itself.

The error occurs because it’s not dt.strptime().

It’s dt.datetime.strptime().

The datetime module contains a datetime object which then contains strptime.

1 Like