1

I would like to create an empty 3D time matrix (with known size) that I will later populate in a loop with either pd.dateTimeIndex or a list of pd.timestamp. Is there a simple method ?

This does not work:

timeMatrix = np.empty( shape=(100, 1000, 2) )
timeMatrix[:] = pd.NaT

I can do without the second line but then the numbers in timeMatrix become 10^18 numbers.

timeMatrix = np.empty( shape=(100, 1000, 2) )
for pressureLevel in levels:
    timeMatrix[ i_airport, 0:varyingNumberBelow1000, pressureLevel ] = dates_datetimeindex

Thank you

PierreL
  • 169
  • 9

1 Answers1

0
df = pd.DataFrame(index=range(10), columns=range(10), dtype="datetime64[ns]")
print(df)

Prints:

    0   1   2   3   4   5   6   7   8   9
0 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
1 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
2 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
3 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
4 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
5 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
6 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
7 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
8 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
9 NaT NaT NaT NaT NaT NaT NaT NaT NaT NaT
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    Thank you. It's not a 3D matrix and I am unsure I can reach it with a dataframe, but with your answer I realize that adding dtype="datetime64[ns]" to my matrix creation leads me close to what I want. I just have 1970 dates as empty dates instead of NaT. I think I can deal with that. – PierreL May 05 '21 at 16:19
  • 1
    To complete the answer, the code to make the 1970 dates as NaT is : timeMatrix[timeMatrix == np.datetime64('1970-01-01T00:00:00.000000000') ] = np.datetime64("NaT") Thank you for the hint :-) – PierreL May 18 '21 at 14:55