[英]How can I create a list of dates from start and end dates in Python?
I have a table of items (in this case various stocks) with start and close dates.我有一个包含开始和结束日期的项目表(在这种情况下是各种库存)。
data = [['Stock A', '2022-01-01', '2022-06-30'], ['Stock B', '2022-01-01', '2022-06-30'], ['Stock C', '2022-01-01', '2022-06-30'], ['Stock D', '2022-01-01', '2022-02-28'], ['Stock E', '2022-03-01', '2022-06-30'], ['Stock F', '2022-05-01', '2022-06-30']]
df_input = pd.DataFrame(data, columns=['stock', 'start date', 'close date'])
which looks something like this:看起来像这样:
Based on this, using Python, I would like to create a table which shows me all stocks that were included on a given date – meaning was <= the date, and their end date was >= the date.基于此,使用 Python,我想创建一个表格,向我显示在给定日期包含的所有股票——意思是 <= 日期,它们的结束日期是 >= 日期。 I would like to format them similarly to this:我想像这样格式化它们:
What would be the right approach to solve this using python?使用python解决这个问题的正确方法是什么?
Edit: Excuse me for not phrasing the question clearly initially and forgetting to add a df.编辑:对不起,我最初没有清楚地表达这个问题并且忘记添加一个 df.
first you have to get the data using pandas首先,您必须使用 pandas 获取数据
df = pd.read_excel(path)
or或者
df = pd.read_csv(path)
then do the date filter on the data然后对数据进行日期过滤
df_date = df.loc[df['start date'].dt.year==year] #to filter the year you want
new_df = df_date.loc[df_date['start date'].dt.month == month] #to create filter for the month you want
I hope this will help you我希望这能帮到您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.