LeMarque
LeMarque

Reputation: 783

python my date conversion function is throwing error while converting the datetime.date format to "yyyy-mm-dd"

I have a list of dates in the following format:

# List of dates
List_of_Dates = [datetime.date(2019, 6, 3), datetime.date(2019, 6, 4), datetime.date(2019, 6, 5), datetime.date(2019, 6, 6), datetime.date(2019, 6, 7), datetime.date(2019, 6, 10), datetime.date(2019, 6, 11), datetime.date(2019, 6, 12)]

And then I defined a function for date conversion:

# define a function for date conversion
def Date_Converter(Date):
    import datetime
    Date2 = datetime.datetime.strptime(Date,"%Y-%m-%d").date()
    return(Date2)

But when I passed the above list of dates to the above function, in the following way:

# return the list of converted dates
Date_Converter(List_of_Dates)

I also tried:

[Date_Converter(x) for x in List_of_Dates]

But in both the cases, I got an error:

 ----> 5     Date2 = datetime.datetime.strptime(Date,"%Y-%m-%d").date()
  6     return(Date2)

 TypeError: strptime() argument 1 must be str, not list

Whereas I was expecting the following list:

["2019-06-03", "2019-06-04", "2019-06-05", "2019-06-06", "2019-06-07", "2019-06-10", "2019-06-11", "2019-06-12"]

Upvotes: 1

Views: 1301

Answers (7)

MEdwin
MEdwin

Reputation: 2960

Another pythonic way is to use the popular map. like this: that will pass the list into the function.

import datetime 
# List of dates
List_of_Dates = [datetime.date(2019, 6, 3), datetime.date(2019, 6, 4), datetime.date(2019, 6, 5), datetime.date(2019, 6, 6), datetime.date(2019, 6, 7), datetime.date(2019, 6, 10), datetime.date(2019, 6, 11), datetime.date(2019, 6, 12)]
# define a function for date conversion
def Date_Converter(Date):    
    Date2 = datetime.datetime.strptime(str(Date),"%Y-%m-%d")
    return(str(Date2.date()))



map(Date_Converter,List_of_Dates) 

result:

    ['2019-06-03',
 '2019-06-04',
 '2019-06-05',
 '2019-06-06',
 '2019-06-07',
 '2019-06-10',
 '2019-06-11',
 '2019-06-12'] 

Upvotes: 0

vanessa orani
vanessa orani

Reputation: 71

Here the code corrected:

def Date_Converter(Date):
return  dt.datetime.strptime(Date,"%Y-%m-%d").date()

output = [Date_Converter(List_of_Dates[a]) for a in 
range(len(List_of_Dates))]

and the output is a list of dates:

['2019-06-03',
 '2019-06-04',
 '2019-06-05',
 '2019-06-06',
 '2019-06-07',
 '2019-06-10',
 '2019-06-11',
 '2019-06-12']

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

There are a lot of issues with your code, let's tackle them one by one.

imports

either import datetime or from datetime import date within one module. Pick one and stick to it, otherwise it will cause confusion.

Creating a new date

datetime.date(2019, 6, 3) is wrong, use datetime(2019, 6, 3) instead.

Update: I was under the assumption that you are useing datetime.datetime.date(), which cannot work, but datetime.date.date() works. Again, using meaningful imports can prevent this.

Trying to parse a datetime object

Instead of strptime (used to parse strings into datetime) you want to use strftime (to convert from datetime to string).

Having a function that expects a single variable and passing it a list of variables

Cannot work obviously. Unless you write the function in such a way that it can handle both single variables and lists.


Here's the fixed code:

from datetime import date

List_of_Dates = [date(2019, 6, 3), date(2019, 6, 4), date(2019, 6, 5)]

def Date_Converter(Date):
    return date.strftime(Date,"%Y-%m-%d")

new_list = [Date_Converter(d) for d in List_of_Dates]

print(new_list)

Output:

['2019-06-03', '2019-06-04', '2019-06-05']

Upvotes: 2

Edel
Edel

Reputation: 92

Using lambda function, you can do like this :

date_Converter = lambda date: [str(date[i]) for i in range(len(date))]

which will return :

date_Converter(List_of_Dates)

['2019-06-03',
 '2019-06-04',
 '2019-06-05',
 '2019-06-06',
 '2019-06-07',
 '2019-06-10',
 '2019-06-11',
 '2019-06-12']

Upvotes: 1

h4z3
h4z3

Reputation: 5458

# return the list of converted dates
Date_Converter(List_of_Dates)

This does nothing with the return value. Also, your names pretty much sum your problem - you pass a list, rather than a single date.

Fix for this line would be:

list_of_converted_dates = [Date_Converter(date) for date in List_of_Dates]

This collects a result for each date from your list.

However, you will encounter another problem after this: TypeError: strptime() argument 1 must be str, not datetime.datetime

This error stems from how you try to convert your date - you do it the other way around: datetime.datetime.strptime(Date,"%Y-%m-%d").date() tries to get a date from a string

Upvotes: 0

ImMJ
ImMJ

Reputation: 206

Use strftime instead of strptime

    import datetime
lis = []
List_of_Dates = [datetime.date(2019, 6, 3), datetime.date(2019, 6, 4), 
                datetime.date(2019, 6, 5), datetime.date(2019, 6, 6), 
                datetime.date(2019, 6, 7), datetime.date(2019, 6, 10), 
                datetime.date(2019, 6, 11), datetime.date(2019, 6, 12)]
for i in List_of_Dates:
    date1 = datetime.datetime.strftime(i,'%Y-%m-%d')
    lis.append(date1)
print lis

Output--

['2019-06-03', '2019-06-04', '2019-06-05', '2019-06-06', '2019-06-07', '2019-06-10', '2019-06-11', '2019-06-12']

Upvotes: 1

The problem is that you pass a list to the function, and it expects a Date.

You can solve it this way I think:

import Datetime

result = []

for date in List_of_Dates:
    result.append(Date_Converter(date))

Upvotes: 0

Related Questions