围绕着将日期转换为datetimeindex的问题太多了。我个人需要一个datetimeindex才能与需要datetimeindex的Calmap包一起使用。在遵循了许多stackoverflow指南之后,我无法将日期字段更改为datetimeindex。这是我采取的以下步骤。
import numpy as np
import pandas as pd
##I also attempted to add parse_dates=["Date'] and Index["Date"] to the pd.read_csv()
main_data = pd.read_csv('newoutput2.csv', delimiter=",", encoding='cp1252')
main_data = main_data.set_index(pd.to_datetime(main_data["Date"], format = "%m/%d/%y"))
import calmap
events = pd.Series(main_data.index)
calmap.yearplot(events, year=2020)
##When I run events[0] the output is
##Timestamp('2020-10-05 00:00:00')
运行该代码后收到的错误是
python\python38-32\lib\site-packages\calmap\__init__.py in yearplot(data, year, how, vmin, vmax, cmap, fillcolor, linewidth, linecolor, daylabels, dayticks, monthlabels, monthticks, ax, **kwargs)
141 # Sample by day.
142 if _pandas_18:
--> 143 by_day = data.resample("D").agg(how)
144 else:
145 by_day = data.resample("D", how=how)
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
无论我采用哪种格式,似乎都不会从数据创建datetimeindex。
数据在这里
原始数据
Name Time Date
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
FName LName 12:00PM 10/5/20
如果我打印main_data,它看起来像这样
Name Time Date
Date
2020-10-05 FName LName 12:00:00 10/5/20
2020-10-05 FName LName 12:00:00 10/5/20
2020-10-05 FName LName 12:00:00 10/5/20
2020-10-05 FName LName 12:00:00 10/5/20
2020-10-05 FName LName 12:00:00 10/5/20
答案 0 :(得分:0)
在设置索引之前尝试concat日期和时间。 Use df.column.str.cat(colum1, sep=' ')
print(df)
Name Time Date
0 FName LName 12:00PM 10/5/20
1 FName LName 12:00PM 10/5/20
2 FName LName 12:00PM 10/5/20
3 FName LName 12:00PM 10/5/20
4 FName LName 12:00PM 10/5/20
5 FName LName 12:00PM 10/5/20
6 FName LName 12:00PM 10/5/20
df['datetime']=pd.to_datetime(df['Date'].str.cat(df.Time, sep=' '))
df.set_index(df['datetime'], inplace=True)
print(df)
Name Time Date datetime
datetime
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00
2020-10-05 12:00:00 FName LName 12:00PM 10/5/20 2020-10-05 12:00:00