如何从 excel 导入日期并使用它?
How to import date from excel and use it?
我有一个包含三列的输入文件:"start date"、"end date" 和 "interval"。输入文件:
start date end date interval
01/01/2020 10/01/2020 15
我想在 pandas date_range
函数中使用这些值。我的
import pandas as pd
timeDF = pd.read_excel('inputFile.xlsx')
startDate = timeDF['start date']
endDate = timeDF['end date']
interval = timeDF['interval']
timeStamp = pd.date_range(start = startDate, end = endDate, freq = str(interval) + 'min')
print(timeStamp)
我遇到的错误:
Name: Interval, dtype: int64min
您 运行 遇到了这个问题,因为您将字符串 "min" 连接到 pandas.core.series.Series 对象的区间上。
当你调用pd.read_excel('inputFile.xlsx')
时,你会得到一个Series对象的字典,所以startDate、endDate和interval都是你应该从中提取数据而不是直接使用的Series对象。
为了解决这个错误,您可以使用 timeDF['name'].values.item() 来获取所有三个变量的值,如下所示:
import pandas as pd
timeDF = pd.read_excel('inputFile.xlsx')
startDate = timeDF['start date'].values.item()
endDate = timeDF['end date'].values.item()
interval = timeDF['interval'].values.item()
timeStamp = pd.date_range(start = startDate, end = endDate, freq = str(interval) + 'min')
print(timeStamp)
输出:
DatetimeIndex(['2020-01-01 00:00:00', '2020-01-01 00:15:00',
'2020-01-01 00:30:00', '2020-01-01 00:45:00',
'2020-01-01 01:00:00', '2020-01-01 01:15:00',
'2020-01-01 01:30:00', '2020-01-01 01:45:00',
'2020-01-01 02:00:00', '2020-01-01 02:15:00',
...
'2020-09-30 21:45:00', '2020-09-30 22:00:00',
'2020-09-30 22:15:00', '2020-09-30 22:30:00',
'2020-09-30 22:45:00', '2020-09-30 23:00:00',
'2020-09-30 23:15:00', '2020-09-30 23:30:00',
'2020-09-30 23:45:00', '2020-10-01 00:00:00'],
dtype='datetime64[ns]', length=26305, freq='15T')
我有一个包含三列的输入文件:"start date"、"end date" 和 "interval"。输入文件:
start date end date interval
01/01/2020 10/01/2020 15
我想在 pandas date_range
函数中使用这些值。我的
import pandas as pd
timeDF = pd.read_excel('inputFile.xlsx')
startDate = timeDF['start date']
endDate = timeDF['end date']
interval = timeDF['interval']
timeStamp = pd.date_range(start = startDate, end = endDate, freq = str(interval) + 'min')
print(timeStamp)
我遇到的错误:
Name: Interval, dtype: int64min
您 运行 遇到了这个问题,因为您将字符串 "min" 连接到 pandas.core.series.Series 对象的区间上。
当你调用pd.read_excel('inputFile.xlsx')
时,你会得到一个Series对象的字典,所以startDate、endDate和interval都是你应该从中提取数据而不是直接使用的Series对象。
为了解决这个错误,您可以使用 timeDF['name'].values.item() 来获取所有三个变量的值,如下所示:
import pandas as pd
timeDF = pd.read_excel('inputFile.xlsx')
startDate = timeDF['start date'].values.item()
endDate = timeDF['end date'].values.item()
interval = timeDF['interval'].values.item()
timeStamp = pd.date_range(start = startDate, end = endDate, freq = str(interval) + 'min')
print(timeStamp)
输出:
DatetimeIndex(['2020-01-01 00:00:00', '2020-01-01 00:15:00',
'2020-01-01 00:30:00', '2020-01-01 00:45:00',
'2020-01-01 01:00:00', '2020-01-01 01:15:00',
'2020-01-01 01:30:00', '2020-01-01 01:45:00',
'2020-01-01 02:00:00', '2020-01-01 02:15:00',
...
'2020-09-30 21:45:00', '2020-09-30 22:00:00',
'2020-09-30 22:15:00', '2020-09-30 22:30:00',
'2020-09-30 22:45:00', '2020-09-30 23:00:00',
'2020-09-30 23:15:00', '2020-09-30 23:30:00',
'2020-09-30 23:45:00', '2020-10-01 00:00:00'],
dtype='datetime64[ns]', length=26305, freq='15T')