0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python]時刻データと数値データの変換メモ

Last updated at Posted at 2020-08-27

数値データの時刻データへの変換

自分用メモ

dataframeでの数値と時系列データの変換に何度も間違えるのでメモ。
以下のようなデータ(秒)を時刻に直すような変換

> df['Time']
0        30620.5
1        39226.0
2        81318.5
3         3438.0
4        11108.5
          ...   
40916     3544.0
40917    37506.0
40918    23386.0
40919    37884.5
40920    24607.5
Name: Time, Length: 40921, dtype: float64
> df['Time']=pd.to_timedelta(df['Time'], unit='S') + pd.to_datetime('1900-01-01')
> df[Time]
0       1900-01-01 08:30:20.500
1       1900-01-01 10:53:46.000
2       1900-01-01 22:35:18.500
3       1900-01-01 00:57:18.000
4       1900-01-01 03:05:08.500
                  ...          
40916   1900-01-01 00:59:04.000
40917   1900-01-01 10:25:06.000
40918   1900-01-01 06:29:46.000
40919   1900-01-01 10:31:24.500
40920   1900-01-01 06:50:07.500
Name: Time, Length: 40921, dtype: datetime64[ns]

Ref : pandas.to_timedelta

unit
str, optional
Denotes the unit of the arg for numeric arg. Defaults to "ns".

Possible values

  • ‘W’
  • ‘D’ / ‘days’ / ‘day’
  • ‘hours’ / ‘hour’ / ‘hr’ / ‘h’
  • ‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’
  • ‘S’ / ‘seconds’ / ‘sec’ / ‘second’
  • ‘ms’ / ‘milliseconds’ / ‘millisecond’ / ‘milli’ / ‘millis’ / ‘L’
  • ‘us’ / ‘microseconds’ / ‘microsecond’ / ‘micro’ / ‘micros’ / ‘U’
  • ‘ns’ / ‘nanoseconds’ / ‘nano’ / ‘nanos’ / ‘nanosecond’ / ‘N’

Changed in version 1.1.0: Must not be specified when arg context strings and errors="raise".

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?