Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index

To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex.to_frame() method. Set the parameter index to False to ignore the index and create a regular DataFrame column instead.

Syntax

DateTimeIndex.to_frame(index=True, name=None)

Parameters

index ? Boolean value. If True (default), uses DateTimeIndex as DataFrame index. If False, creates a regular column.

name ? Column name for the DataFrame. If None, uses the DateTimeIndex name or defaults to 0.

Creating DateTimeIndex

First, let's create a DateTimeIndex with timezone and frequency ?

import pandas as pd

# Create DatetimeIndex with period 5 and frequency 40 seconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

print("DateTimeIndex...")
print(datetimeindex)
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')

Converting to DataFrame with Index

By default, to_frame() uses the DateTimeIndex as the DataFrame index ?

import pandas as pd

datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

# Convert to DataFrame with DateTimeIndex as index (default behavior)
df_with_index = datetimeindex.to_frame()
print("DataFrame with DateTimeIndex as index:")
print(df_with_index)
DataFrame with DateTimeIndex as index:
                                    0
2021-10-18 07:20:32.261811624+10:30 2021-10-18 07:20:32.261811624+10:30
2021-10-18 07:21:12.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:21:52.261811624+10:30
2021-10-18 07:22:32.261811624+10:30 2021-10-18 07:22:32.261811624+10:30
2021-10-18 07:23:12.261811624+10:30 2021-10-18 07:23:12.261811624+10:30

Converting to DataFrame Ignoring Index

Set index=False to create a regular DataFrame column with integer index ?

import pandas as pd

datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

# Convert to DataFrame ignoring the DateTimeIndex as index
df_ignore_index = datetimeindex.to_frame(index=False)
print("DataFrame ignoring DateTimeIndex:")
print(df_ignore_index)
print(f"\nDataFrame shape: {df_ignore_index.shape}")
DataFrame ignoring DateTimeIndex:
                                   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

DataFrame shape: (5, 1)

Custom Column Name

You can specify a custom column name using the name parameter ?

import pandas as pd

datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

# Convert to DataFrame with custom column name
df_custom_name = datetimeindex.to_frame(index=False, name='timestamp')
print("DataFrame with custom column name:")
print(df_custom_name)
DataFrame with custom column name:
                            timestamp
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

Comparison

Parameter Result Use Case
index=True (default) DateTimeIndex becomes DataFrame index Time-series analysis
index=False Regular DataFrame with integer index Data processing, merging

Conclusion

Use to_frame(index=False) to convert DateTimeIndex to a regular DataFrame column with integer indexing. This is useful when you need standard DataFrame operations without time-based indexing.

Updated on: 2026-03-26T17:35:14+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements