英文:
Pandas shift index ignoring passed shift object
问题
Here's the translated portion of your text:
让我们假设我有一个名为df的数据框如下所示
import pandas as pd
df = pd.date_range('2023-04-01', '2023-05-01')
frequency = df.shift(freq='W')
print(frequency)
我得到的输出中频率为None
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-09', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-16',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-23', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-04-30', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-05-07', '2023-05-07'],
dtype='datetime64[ns]', freq=None) <<<<<<--------这里------<<<<<<
根据[文档][1],`W`代表周
我有什么遗漏吗?我在寻找快速解决办法...是否有其他方法?
版本:1.4.2
[![在此输入图像描述][2]][2]
[1]: https://pandas.pydata.org/pandas-docs/version/0.9.1/timeseries.html#offset-aliases
[2]: https://i.stack.imgur.com/2akOD.png
<details>
<summary>英文:</summary>
Let's say I've df like this
import pandas as pd
df= pd.date_range('2023-04-01', '2023-05-01')
frequency = df.shift(freq='W')
print(frequency)
output I got freuqnecy as `None`
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-09', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-16',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-23', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-04-30', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-05-07', '2023-05-07'],
dtype='datetime64[ns]', freq=None) <<<<<<<<--------Here------<<<<<
According to [documentation][1] `W` stands for week
Am i missing anything here?? I was looking for a quick fix..Is there any alternate way to do it?
Version: 1.4.2
[![enter image description here][2]][2]
[1]: https://pandas.pydata.org/pandas-docs/version/0.9.1/timeseries.html#offset-aliases
[2]: https://i.stack.imgur.com/2akOD.png
</details>
# 答案1
**得分**: 1
I am not sure what you are trying to do, but it gives you the expected outcome.
According to the [documentation][1]:
```plaintext
month_starts = pd.date_range('1/1/2011', periods=5, freq='MS')
month_starts
DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01',
'2011-05-01'],
dtype='datetime64[ns]', freq='MS')
month_starts.shift(10, freq='D')
DatetimeIndex(['2011-01-11', '2011-02-11', '2011-03-11', '2011-04-11',
'2011-05-11'],
dtype='datetime64[ns]', freq=None)
It produces the right outcome.
In your case the original df
:
DatetimeIndex(['2023-04-01', '2023-04-02', '2023-04-03', '2023-04-04',
'2023-04-05', '2023-04-06', '2023-04-07', '2023-04-08',
'2023-04-09', '2023-04-10', '2023-04-11', '2023-04-12',
'2023-04-13', '2023-04-14', '2023-04-15', '2023-04-16',
'2023-04-17', '2023-04-18', '2023-04-19', '2023-04-20',
'2023-04-21', '2023-04-22', '2023-04-23', '2023-04-24',
'2023-04-25', '2023-04-26', '2023-04-27', '2023-04-28',
'2023-04-29', '2023-04-30', '2023-05-01'],
dtype='datetime64[ns]', freq='D')
is converting to the next week (always the Sunday of the week):
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-09', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-16',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-23', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-04-30', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-05-07', '2023-05-07'],
dtype='datetime64[ns]', freq=None)
He gives back the frequency none because the datapoints don't have a frequency. You could clean the "duplicates" and then you have your preferred frequency:
print(frequency.drop_duplicates())
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-16', '2023-04-23',
'2023-04-30', '2023-05-07'],
dtype='datetime64[ns]', freq=None)
but it will not detect the frequency then.
英文:
I am not sure what you are trying to do, but it gives you the expected outcome.
According to the documentation:
month_starts = pd.date_range('1/1/2011', periods=5, freq='MS')
month_starts
DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01',
'2011-05-01'],
dtype='datetime64[ns]', freq='MS')
month_starts.shift(10, freq='D')
DatetimeIndex(['2011-01-11', '2011-02-11', '2011-03-11', '2011-04-11',
'2011-05-11'],
dtype='datetime64[ns]', freq=None)
It produces the right outcome.
In your case the original df
:
DatetimeIndex(['2023-04-01', '2023-04-02', '2023-04-03', '2023-04-04',
'2023-04-05', '2023-04-06', '2023-04-07', '2023-04-08',
'2023-04-09', '2023-04-10', '2023-04-11', '2023-04-12',
'2023-04-13', '2023-04-14', '2023-04-15', '2023-04-16',
'2023-04-17', '2023-04-18', '2023-04-19', '2023-04-20',
'2023-04-21', '2023-04-22', '2023-04-23', '2023-04-24',
'2023-04-25', '2023-04-26', '2023-04-27', '2023-04-28',
'2023-04-29', '2023-04-30', '2023-05-01'],
dtype='datetime64[ns]', freq='D')
is converting to the next week (always the sunday of the week):
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-09', '2023-04-09', '2023-04-09', '2023-04-09',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-16',
'2023-04-16', '2023-04-16', '2023-04-16', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-23', '2023-04-23',
'2023-04-23', '2023-04-23', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-04-30', '2023-04-30', '2023-04-30',
'2023-04-30', '2023-05-07', '2023-05-07'],
dtype='datetime64[ns]', freq=None)
He gives back the frequency none because the datapoints don't have a frequency. You could clean the "duplicates" and then you have your preferred frequency:
print(frequency.drop_duplicates())
DatetimeIndex(['2023-04-02', '2023-04-09', '2023-04-16', '2023-04-23',
'2023-04-30', '2023-05-07'],
dtype='datetime64[ns]', freq=None)
but it will not detect the frequency then.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论