亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pandas - 日期時間列的奇怪問題

Pandas - 日期時間列的奇怪問題

PHP
子衿沉夜 2023-11-09 15:48:02
我對日期時間列有一個奇怪的問題。假設 start_date 列中有一個日期:>>> df2.info()<class 'pandas.core.frame.DataFrame'>Int64Index: 641 entries, 9 to 1394Data columns (total 2 columns): #   Column      Non-Null Count  Dtype         ---  ------      --------------  -----          0   number      641 non-null    object         1   start_date  641 non-null    datetime64[ns]dtypes: datetime64[ns](1), object(1)memory usage: 15.0+ KB當我將索引設置為start_date時,DatetimeIndex似乎不完整:>>> df2 = df2.set_index('start_date')>>> df2.info()<class 'pandas.core.frame.DataFrame'>DatetimeIndex: 641 entries, 2020-01-01 to 2020-03-01Data columns (total 1 columns): #   Column  Non-Null Count  Dtype ---  ------  --------------  -----  0   number  641 non-null    objectdtypes: object(1)memory usage: 10.0+ KB實際上這個數據框中還有更多條目:df3 = df2.copy()df3 = df3.reset_index()df3 = df3[pd.to_datetime(df3['start_date']).dt.month > 3]df3 = df3.set_index('start_date')df3.info()<class 'pandas.core.frame.DataFrame'>DatetimeIndex: 393 entries, 2020-04-01 to 2020-09-01Data columns (total 1 columns): #   Column  Non-Null Count  Dtype ---  ------  --------------  -----  0   number  393 non-null    objectdtypes: object(1)memory usage: 6.1+ KB正如您所看到的,有日期截至 的條目2020-09-01。但為什么有時只給出這些日期呢?我無法在索引 start_date 中檢測到間隙或類似的內容。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

DataFrame.info打印出XXX to YYY索引中的信息時,它只是打印出第一個索引值的值到最后一個索引值的值。如果您的索引不是單調的(可以使用 輕松檢查df.index.is_monotonic),則這不對應于完整范圍。

負責此操作的代碼是Index._summary,很明顯,它在總結時只是查看第一個值[0]和最后一個值[-1]

def _summary(self, name=None) -> str_t:

? ? """

? ? Return a summarized representation.

? ? Parameters

? ? ----------

? ? name : str

? ? ? ? name to use in the summary representation

? ? Returns

? ? -------

? ? String with a summarized representation of the index

? ? """

? ? if len(self) > 0:

? ? ? ? head = self[0]

? ? ? ? if hasattr(head, "format") and not isinstance(head, str):

? ? ? ? ? ? head = head.format()

? ? ? ? tail = self[-1]

? ? ? ? if hasattr(tail, "format") and not isinstance(tail, str):

? ? ? ? ? ? tail = tail.format()

? ? ? ? index_summary = f", {head} to {tail}"

? ? else:

? ? ? ? index_summary = ""

這是一個簡單的例子:


import pandas as pd


df = pd.DataFrame(data=[1,1,1], index=pd.to_datetime(['2010-01-01', '2012-01-01', '2011-01-01']))


df.info()


#<class 'pandas.core.frame.DataFrame'>

#DatetimeIndex: 3 entries, 2010-01-01 to 2011-01-01

#...

sort如果您想在查看信息之前了解完整的索引:


df.sort_index().info()

#<class 'pandas.core.frame.DataFrame'>

#DatetimeIndex: 3 entries, 2010-01-01 to 2012-01-01

#...


查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號