Convert dd, mm, yy arrays to numpy datetime

PetGriffin

I am trying to convert separate arrays of YY, MM, DD values to a numpy datetime64 array. Here is the code

import numpy as np

iyy = [2020, 2020, 2019, 2018, 2017]
imm = [2, 4, 4, 6, 8]
idd = [1, 2, 3, 4, 5]

isod = []
for y, m, d in zip(iyy, imm, idd):
    isod.append("%4d-%02d-%02d" % (y, m, d))

t1 = np.datetime64(np.asarray(isod))

This gives the error Could not convert object to NumPy datetime. Would appreciate any suggestions for achieving this. Thanks in advance

Arty

Change

np.datetime64(np.asarray(isod))

to

np.asarray(isod).astype(np.datetime64)

Full code:

Try it online!

import numpy as np

iyy = [2020, 2020, 2019, 2018, 2017]
imm = [2, 4, 4, 6, 8]
idd = [1, 2, 3, 4, 5]

isod = []
for y, m, d in zip(iyy, imm, idd):
    isod.append("%4d-%02d-%02d" % (y, m, d))

t1 = np.asarray(isod).astype(np.datetime64)
print(t1)

Output:

['2020-02-01' '2020-04-02' '2019-04-03' '2018-06-04' '2017-08-05']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert MM/YY to YYYY-MM-DD in MYSQL

Convert time in millis since epoch to "mm/dd/yy"

Convert date with format dd.mm.yy to yyyy-mm-dd in python

oracle convert DD-MON-YY to DD/MM/YYYY

Pandas, convert datetime format mm/dd/yyyy to dd/mm/yyyy

How to change/convert input date format from MM dd yy to yyyy-mm-dd on submit

How to convert numpy datetime into the format "MM-DD"

Convert DD-MMM-YY to YYYY/MM/DD in Pandas

Convert mm-dd-yy, hr-mm-ss to yy-mm-dd timestamp in pyspark

The simplest way to convert a long date format to YY/MM/DD HTML

convert text file column field with data dd.mm.yy to dd.mm.YYYY

Excel - convert part of filename to mm/dd/yy date

Excel - Convert date from yymmdd to dd/mm/yy

How to convert date dd/mm/YY to mm-dd-YY format in PHP

want to convert string mm/dd/yy to date yyyy/mm/dd format in Sybase

convert epoch time into dd/mm/yy in a file

How do I convert a DateTime variable to return date in the 'mm/dd/yy' format

Convert a character in the form of DD-MMM-YY to DD/MM/YY in R

Regular expression convert Date format from dd-mon-yy to dd/MM/yy

GoogleFinace convert dd/mm/yy to yy,mm,dd format

Convert mm-dd-YY to Timestamp at Midnight UTC

How to convert excel dates dd/mm/yy into number format such as 01011994

How to convert the given date to dd-mm-yy format in javascript?

SQL query to convert the value to MM/DD/YY and HH:MM:SS

how to convert date to yy-mm-dd in scala

How to convert date format "dd/mm/yy" to "yy/mm/dd" for mysql database inserting?

Convert a formmated string (dd/mm/yy) to and ISO datetime

Convert Timestamp string (dd MMM yyyy HH:mm:ss) to Date (DD/MM/YY) in Google Sheets

Convert timestamp to "YY/MM/DD" with JS