Để tạo DataFrame từ DateTimeIndex bỏ qua chỉ mục, hãy sử dụng phương thức DateTimeIndex.to_frame (). Đặt tham số chỉ mục thành Sai để bỏ qua chỉ mục.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Tạo một DatetimeIndex với chu kỳ 5 và tần suất là S tức là giây -
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S') Hiển thị DateTimeIndex -
print("DateTimeIndex...\n", datetimeindex)
Tạo DataFrame từ DateTimeIndex. Chỉ mục ban đầu không được đặt trong DataFrame được trả về bằng cách sử dụng tham số 'Sai' -
print("\nDateTimeIndex to DataFrame...\n", datetimeindex.to_frame(index=False)) Ví dụ
Sau đây là mã -
import pandas as pd
# DatetimeIndex with period 5 and frequency as S i.e. seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
# Create a DataFrame from DateTimeIndex
# The original index isn't set in the returned DataFrame using the 'False' parameter
print("\nDateTimeIndex to DataFrame...\n", datetimeindex.to_frame(index=False)) Đầu ra
Điều này sẽ tạo ra mã sau -
DateTimeIndex... DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30', '2021-10-18 07:21:12.261811624+10:30', '2021-10-18 07:21:52.261811624+10:30', '2021-10-18 07:22:32.261811624+10:30', '2021-10-18 07:23:12.261811624+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='40S') DateTimeIndex frequency... <40 * Seconds> DateTimeIndex to DataFrame... 0 0 2021-10-18 07:20:32.261811624+10:30 1 2021-10-18 07:21:12.261811624+10:30 2 2021-10-18 07:21:52.261811624+10:30 3 2021-10-18 07:22:32.261811624+10:30 4 2021-10-18 07:23:12.261811624+10:30