Convert date in pandas

时间:2017-08-05 11:27:48

标签: python pandas

I know this has been asked like 100 times but I still don't get it and the given solutions don't get me anywhere.

Im trying to convert time into a comparable format with Pandas/Python. I used a db entries as data and currently I have trouble using time like this:

52   2017-08-04 12:26:56.348698
53   2017-08-04 12:28:22.961560
54   2017-08-04 12:34:20.299041

the goal is to use it as year1 and year2 to make a graph like:

def sns_compare(year1,year2):
    f, (ax1) = plt.subplots(1, figsize=LARGE_FIGSIZE)
    for yr in range(int(year1),int(year2)):
        sns.distplot(tag2[str(yr)].dropna(), hist=False, kde=True, rug=False, bins=25)
sns_compare(year1,year2)

When I try to to it like this I get ValueError: invalid literal for int() with base 10: '2017-08-04 12:34:20.299041'.

So currently I think about using Regex to manipulate the time fields but this cant be the way to go or at least I cant imagine. I tried all kind of suggestions from SO/GitHub but nothing really worked. I also don't know what the "optimal" time structure should look like. Is it 20170804123420299041 or something like 2017-08-04-12-34-20-299041. I hope somebody can make this clear to me.

2 个答案:

答案 0 :(得分:1)

你看过pd.to_datetime了吗? Pandas和Seaborn应该能够很好地处理日期,你不必将它们转换成整数。

答案 1 :(得分:1)

这是您的数据:

from matplotlib import pyplot as plt
from datetime import datetime
import pandas as pd
df = pd.DataFrame([("2017-08-04 12:26",56.348698),("2017-08-04 12:28",22.961560),("2017-08-04 12:34",20.299041)])
df.columns = ["date", "val"]

首先,我们转换为日期时间,然后我们减少year1,接下来我们转换为天。

df['date'] = pd.to_datetime(df["date"])
df["days"]=(df['date'] -datetime(year1,1,1)).dt.total_seconds()/86400.0

绘制数据,并仅显示year1year2

之间的天数
plt.scatter(df["days"],df["val"])
plt.xlim((0,(year2-year1)*365))
plt.show()