Working with Dates and Times in Python: A Guide to 'datetime64[ns]' and '<M8[ns]>'
Here's a breakdown of the key points:
Key Differences (Nuances):
Practical Usage:
You'll typically encounter
'<M8[ns]>'
when working with date and time data in Pandas DataFrames. Pandas seamlessly handles these timestamps for various operations like:- Time zone conversions
- Date/time arithmetic (adding/subtracting days, hours, etc.)
- Date/time comparisons
import numpy as np
import pandas as pd
# Using `datetime64[ns]` directly (NumPy)
np_datetime = np.datetime64('2023-12-31', dtype='datetime64[ns]')
print(np_datetime) # Output: np.datetime64('2023-12-31T00:00:00.000000000')
# Using `pd.to_datetime` (Pandas) - creates '<M8[ns]>'
pd_datetime = pd.to_datetime('2024-07-04')
print(pd_datetime) # Output: 2024-07-04 00:00:00.000000000 (dtype: '<M8[ns]')
# Alternative using pandas Series (also '<M8[ns]>')
pd_series = pd.Series(['2023-10-26'])
print(pd_series.dtype) # Output: dtype('<M8[ns]')
Checking data type:
print(np_datetime.dtype) # Output: datetime64[ns]
print(pd_datetime.dtype) # Output: datetime64[ns] (even though it was created with '<M8[ns]>' string)
As you can see, Pandas automatically converts the string representation ('<M8[ns]>'
) to the underlying NumPy datetime64[ns]
data type when working within the DataFrame or Series.
Using timestamps in Pandas:
df = pd.DataFrame({'date': ['2024-01-01', '2024-02-15', '2024-03-31']})
df['date'] = pd.to_datetime(df['date']) # Convert to datetime
# Timezone conversion (example)
df['utc_date'] = df['date'].dt.tz_localize(None).dt.tz_convert('UTC')
# Time difference (example)
time_diff = df['date'].iloc[2] - df['date'].iloc[0]
print(time_diff) # Output: Timedelta(days=89, hours=0)
import numpy as np
# Specify reference point (epoch)
epoch_datetime = np.datetime64(0, 'D') # January 1st, 1970
specific_datetime = epoch_datetime + np.timedelta64(10, 'D') # 10 days after epoch
# Specify units other than nanoseconds
datetime_in_seconds = np.datetime64('2024-07-06', 's') # Timestamp in seconds
print(specific_datetime) # Output: np.datetime64('1970-01-10T00:00:00.000000000')
print(datetime_in_seconds) # Output: np.datetime64('2024-07-06T00:00:00.000000000')
Using pd.Timestamp:
import pandas as pd
# Create timestamp from string
timestamp_from_string = pd.Timestamp('2024-07-06')
# Create timestamp from another datetime object
datetime_object = datetime.datetime(2023, 12, 25)
timestamp_from_datetime = pd.Timestamp(datetime_object)
print(timestamp_from_string) # Output: 2024-07-06 00:00:00.000000000
print(timestamp_from_datetime) # Output: 2023-12-25 00:00:00.000000000
Using datetime.datetime (standard library):
While not directly related to datetime64[ns]
or '<M8[ns]>'
, you can use the datetime
module to create standard datetime objects:
import datetime
# Create datetime object
current_datetime = datetime.datetime.now()
# Access specific components
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day
print(current_datetime) # Output: something like 2024-07-06 00:36:22.754234
print(f"Year: {year}, Month: {month}, Day: {day}")
python numpy pandas