【问题标题】:How to convert two columns from decimal years to date如何将两列从十进制年份转换为日期
【发布时间】:2019-07-20 01:50:09
【问题描述】:

我是 Python 新手,但遇到了问题。

我在 .txt 文档中有两列十进制年份的数据,我想将两列中的每个数字转换为数据 (yyyy-mm-dd)

2014.16020 2019.07190
2000.05750 2019.10750
2001.82610 2019.10750
2010.36280 2019.07190
2005.24570 2019.10750
2015.92610 2019.10750
2003.43600 2014.37100

然后从第一列的数据中减去第二列的数据,得到两个数据之间的天数。 例如结果应该是这样的:

1825
3285
2920
3283
ecc..

【问题讨论】:

  • 什么是小数年?我不明白它是如何转换为日期的。

标签: python datetime converters date-difference


【解决方案1】:

提供文件和输出文件的正确路径。下面的代码将完成其余的工作。

import pandas as pd
import numpy as np
df=pd.read_csv('your_file.txt',delimiter=' ',header=None,parse_dates=[0,1])
df['date_diffrence']=((df[1]-df[0])/np.timedelta64(1,'D')).astype(int)
df.to_csv('your_file_result.txt',header=None,sep=' ',index=False)

【讨论】:

    【解决方案2】:

    我把我的解释放在下面的代码中

    from datetime import datetime  # yes they named a class the same as module
    x = '''2014.16020 2019.07190
    2000.05750 2019.10750
    2001.82610 2019.10750
    2010.36280 2019.07190
    2005.24570 2019.10750
    2015.92610 2019.10750
    2003.43600 2014.37100'''
    
    # split input into lines. Assumption here is that there is one pair of dates per each line
    lines = x.splitlines() 
    # set up a container (list) for outputs
    deltas = []
    # process line by line
    for line in lines:
        # split line into separate dates
        inputs = line.split()
        dates = []
        for input in inputs:
            # convert text to number
            date_decimal = float(input)
            # year is the integer part of the input
            date_year = int(date_decimal)
            # number of days is part of the year, which is left after we subtract year
            year_fraction = date_decimal - date_year
            # a little oversimplified here with int and assuming all years have 365 days
            days = int(year_fraction * 365)
            # now convert the year and days into string and then into date (there is probably a better way to do this - without the string step)
            date = datetime.strptime("{}-{}".format(date_year, days),"%Y-%j")  
            # see https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior for format explanation
            dates.append(date)
        deltas.append(dates[1] - dates[0])
    
    # now print outputs
    for delta in deltas:
        print(delta.days)
    

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 2020-12-05
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2015-11-10
      • 2020-12-21
      相关资源
      最近更新 更多