質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

1142閲覧

pandasにてperiods.to_timestamp('D', 'end')の'D'や'end'の意味を教えて下さい

ao_tombo

総合スコア7

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2021/07/23 06:24

オライリーのPythonによるデータ分析入門第2版なのですが、p271に

python3

1data = pd.read_csv('examples/macrodata.csv') 2periods = pd.PeriodIndex(year=data.year, quarter=data.quarter, name='date) 3data.index = periods.to_timestamp('D', 'end')

と書かれていますが、この'D'や'end'は何を意味しているのでしょうか?
このページに引数について書かれているのですが、これを読んでも言っていることが分かりません。
宜しくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こういうのを全部質問していたらきりがないので、調べ方を覚えましょう。

まず、PeriodIndexのドキュメント文字列を見ます。

python

1>>> print(pd.PeriodIndex.__doc__) 2 3 Immutable ndarray holding ordinal values indicating regular periods in time. 4 5 Index keys are boxed to Period objects which carries the metadata (eg, 6 frequency information). 7 8 Parameters 9 ---------- 10 data : array-like (1d int np.ndarray or PeriodArray), optional 11 Optional period-like data to construct index with. 12 copy : bool 13 Make a copy of input ndarray. 14 freq : str or period object, optional 15 One of pandas period strings or corresponding objects. 16 year : int, array, or Series, default None 17 month : int, array, or Series, default None 18 quarter : int, array, or Series, default None 19 day : int, array, or Series, default None 20 hour : int, array, or Series, default None 21 minute : int, array, or Series, default None 22 second : int, array, or Series, default None 23 tz : object, default None 24 Timezone for converting datetime64 data to Periods. 25 dtype : str or PeriodDtype, default None 26 27 Attributes 28 ---------- 29 day 30 dayofweek 31 day_of_week 32 dayofyear 33 day_of_year 34 days_in_month 35 daysinmonth 36 end_time 37 freq 38 freqstr 39 hour 40 is_leap_year 41 minute 42 month 43 quarter 44 qyear 45 second 46 start_time 47 week 48 weekday 49 weekofyear 50 year 51 52 Methods 53 ------- 54 asfreq 55 strftime 56 to_timestamp 57 58 See Also 59 -------- 60 Index : The base pandas Index type. 61 Period : Represents a period of time. 62 DatetimeIndex : Index with datetime64 data. 63 TimedeltaIndex : Index of timedelta64 data. 64 period_range : Create a fixed-frequency PeriodIndex. 65 66 Examples 67 -------- 68 >>> idx = pd.PeriodIndex(year=[2000, 2002], quarter=[1, 3]) 69 >>> idx 70 PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]', freq='Q-DEC')

これを元に、動かしてみましょう。

python

1>>> periods = pd.PeriodIndex(year = [2001, 2001, 2002, 2002], quarter=[3,4,1,2], name='date') 2>>> print(periods) 3PeriodIndex(['2001Q3', '2001Q4', '2002Q1', '2002Q2'], dtype='period[Q-DEC]', name='date', freq='Q-DEC')

次に、to_timestampのドキュメント文字列を見ます。

python

1print(periods.to_timestamp.__doc__) 2 3>>> print(periods.to_timestamp.__doc__) 4 5Cast to DatetimeArray/Index. 6 7Parameters 8---------- 9freq : str or DateOffset, optional 10 Target frequency. The default is 'D' for week or longer, 11 'S' otherwise. 12how : {'s', 'e', 'start', 'end'} 13 Whether to use the start or end of the time period being converted. 14 15Returns 16------- 17DatetimeArray/Index

これを元に、動かしてみましょう。

python

1>>> data_index = periods.to_timestamp('D', 'end') 2>>> print(data_index) 3DatetimeIndex(['2001-09-30 23:59:59.999999999', 4 '2001-12-31 23:59:59.999999999', 5 '2002-03-31 23:59:59.999999999', 6 '2002-06-30 23:59:59.999999999'], 7 dtype='datetime64[ns]', name='date', freq=None)

投稿2021/07/23 06:53

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ao_tombo

2021/07/23 08:32

ありがとうございました。参考になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問