Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Pandas - 시간대 정보가 포함된 python datetime.time 객체의 numpy 배열 반환

<시간/>

시간대 정보가 있는 python datetime.time 객체의 numpy 배열을 반환하려면 datetimeindex.timetz를 사용하세요. 재산.

먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

마침표가 5이고 빈도가 나노초인 DatetimeIndex를 생성합니다(예:나노초 −

).
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns')

DateTimeIndex 표시 -

print("DateTimeIndex...\n", datetimeindex)

시간대 정보가 있는 Timestamp의 시간 부분만 반환 -

print("\nThe numpy array (time part with timezone)..\n",datetimeindex.timetz)

다음은 코드입니다 -

import pandas as pd

# DatetimeIndex with period 5 and frequency as us i.e. nanoseconds
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# Returns only the time part of Timestamp with timezone information
print("\nThe numpy array (time part with timezone)..\n",datetimeindex.timetz)

출력

이것은 다음 코드를 생성합니다 -

DateTimeIndex...
DatetimeIndex([ '2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00',
'2021-10-20 02:30:50.000000003+11:00',
'2021-10-20 02:30:50.000000004+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='N')

The numpy array (time part with timezone)..
[datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)]