2

my present array is given below:

 print(alldates)

Its output:

array([datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),
       datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),
       datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)], dtype=object)

I want to convert it to something like this:

alldates = ['2019-01-25'.....,'2019-02-01']

3 Answers 3

3

Use an .astype(str):

print(alldates.astype(str))

Which outputs:

['2019-01-25' '2019-01-26' '2019-01-27' '2019-01-29' '2019-01-31'
 '2019-02-01']
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, could you take a look at this question stackoverflow.com/questions/70954791/…
1

You can achieve the result by using below code,

datelist = [datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)]

resultlist = [i.strftime('%d-%m-%Y') for i in datelist]

print(resultlist)

Result :

['25-01-2019', '26-01-2019', '27-01-2019', '29-01-2019', '31-01-2019', '01-02-2019']

Comments

0

you can use str() transfer to string and then append to new array

import datetime

alldates=[datetime.date(2019, 1, 25), datetime.date(2019, 1, 26),datetime.date(2019, 1, 27), datetime.date(2019, 1, 29),datetime.date(2019, 1, 31), datetime.date(2019, 2, 1)]

new_allldates = []
for item in alldates:
    new_allldates.append(str(item))

print(new_allldates)

Result:

['2019-01-25', '2019-01-26', '2019-01-27', '2019-01-29', '2019-01-31', '2019-02-01']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.