Skip to content

DatetimeIndex freq parameter is inconsistent when passing nanosecond frequencies #3524

Closed
@cpcloud

Description

@cpcloud

This seems like a bug:

import pandas as pd
import numpy as np

def create_ns_index(start, fs, nsamples, name='time'):
    ns = int(1e9 / fs)
    dtstart = np.datetime64(start)
    dt = dtstart + np.arange(nsamples) * np.timedelta64(ns, 'ns')
    freq = ns * pd.datetools.Nano()
    return pd.DatetimeIndex(dt, freq=freq, name=name)

index = create_ns_index('2012-09-20T00:00:00', 24414, 400)
assert index.freq == '40960N'
assert index.inferred_freq == '40960N'

# try to create the same index using the period string
index = pd.DatetimeIndex(start=index[0], end=index[-1], freq=index.freq)
# throws an AttributeError

# now try to create the same index using date_range
index = pd.date_range(start=index[0], end=index[-1], freq=index.freq)
# throws the same AttributeError

# maybe it works if I use pd.datetools.Nano()
index = pd.DatetimeIndex(start=index[0], end=index[-1], freq=index.freq.n * pd.datetools.Nano())
# no such luck

# try to use periods...also fails with an ambiguous ValueError
index = pd.date_range(start=index[0], end=index[-1], periods=index.size)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions