[파이썬][numpy] numpy numpy에서 datetime64 사용

NumPy is a popular library in Python for numerical computing. Along with its powerful array manipulation capabilities, NumPy also provides support for working with dates and times through its datetime64 data type. In this blog post, we will explore how to use datetime64 in NumPy and how it can be beneficial in various data analysis and manipulation tasks.

What is datetime64?

The datetime64 data type in NumPy represents dates or times as 64-bit integers. This data type allows efficient storage and manipulation of date and time information, with support for a wide range of precision, from as small as nanoseconds up to centuries. It is particularly useful when dealing with large datasets that require high-performance operations involving dates and times.

Creating datetime64 Arrays

To create a datetime64 array in NumPy, we can use the numpy.array() function and pass the desired date or time strings as elements. The array will automatically be assigned the datetime64 data type. Let’s look at an example:

import numpy as np

dates = np.array(['2021-01-01', '2021-02-01', '2021-03-01'], dtype='datetime64')
print(dates)

Output:

['2021-01-01' '2021-02-01' '2021-03-01']

In the above code, we create a datetime64 array dates containing three date strings. The dtype='datetime64' argument specifies the data type as datetime64.

Date Arithmetic with datetime64

One of the primary benefits of using datetime64 in NumPy is the ability to perform date arithmetic. We can perform operations like addition, subtraction, and comparison on datetime64 arrays.

import numpy as np

dates = np.array(['2021-01-01', '2021-02-01', '2021-03-01'], dtype='datetime64')

# Addition
future_date = dates + np.timedelta64(7, 'D')
print(future_date)

# Subtraction
time_difference = dates[1] - dates[0]
print(time_difference)

# Comparison
is_after = dates[2] > dates[1]
print(is_after)

Output:

['2021-01-08' '2021-02-08' '2021-03-08']
31 days, 0:00:00
True

In the code above, we first create a datetime64 array dates. We then demonstrate three operations:

Reshaping datetime64 Arrays

NumPy provides several functions and methods to reshape datetime64 arrays. These operations can come in handy when you need to organize or reformat your date and time data for further analysis.

import numpy as np

dates = np.array(['2021-01-01', '2021-02-01', '2021-03-01'], dtype='datetime64')

# Reshape to different dimensions
reshaped_dates = dates.reshape((3, 1))
print(reshaped_dates)

# Generate a range of dates
date_range = np.arange('2021-01-01', '2021-02-01', dtype='datetime64')
print(date_range)

Output:

[['2021-01-01']
 ['2021-02-01']
 ['2021-03-01']]
['2021-01-01' '2021-01-02' '2021-01-03' '2021-01-04' '2021-01-05'
 '2021-01-06' '2021-01-07' '2021-01-08' '2021-01-09' '2021-01-10'
 '2021-01-11' '2021-01-12' '2021-01-13' '2021-01-14' '2021-01-15'
 '2021-01-16' '2021-01-17' '2021-01-18' '2021-01-19' '2021-01-20'
 '2021-01-21' '2021-01-22' '2021-01-23' '2021-01-24' '2021-01-25'
 '2021-01-26' '2021-01-27' '2021-01-28' '2021-01-29' '2021-01-30']

In the above code, we demonstrate two reshaping operations:

Conclusion

In this blog post, we explored how to use the datetime64 data type in NumPy for working with dates and times in Python. We learned how to create datetime64 arrays, perform date arithmetic, and reshape the arrays for further analysis. The datetime64 data type in NumPy provides efficient and convenient functionality for handling date and time information, making it a valuable tool for various data analysis tasks.