[Tutor] Datetime Integer Array
Hello, I am reading a data file with a string time stamp as the first column, example below: '03/10/2010 02:00:00' '03/10/2010 02:10:00' '03/10/2010 02:20:00' '03/10/2010 02:30:00' etc to n number of rows. I'm using the numpy function genfromtxt to read this data: import numpy as np datetime_IN = np.genfromtxt('SIL633_original.txt', delimiter='\t', skip_header=141, dtype='|S19', usecols=0) Now I have a variable called datetime_IN which is an array of datetime strings to the nth row. I'd like to convert this strings to a numpy array of integers where each column represents a value of time. For example, using the same values above, i want an array of integers to look like this: 3,10,2010,2,0,0 3,10,2010,2,10,0 3,10,2010,2,20,0 3,10,2010,2,30,0 etc to n number of rows. I have already tried creating a numpy array of integers using this code: import time time_format = %m/%d/%Y %H:%M:%S for x in range(len(datetime_IN)): junk = time.strptime(datetime[x],time_format) junk2 = [y for y in junk] The above code works in general but it doesn't create an array with the same number of rows as datetime_IN, and I understand it doesn't because the previous data in junk2 is lost. I'd like to build the junk2 array but I'm not sure how. In other languages, I'm able to iterate over an array to build it, so in each iteration of a loop a new row is created for the arrray I'm building. I'm not quite sure how to do that in python. thanks -- jt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Datetime Integers
Hello, Is there a module available for python to convert datetime into an array of integers. For example, I have date where the first column is a datetime string (i.e. '2010-10-10 01:10:00') and I would like to convert that into an array with 5 columns corresponding to the integer values of Year,Month,Day,Hour,Minute. There is a function in Matlab that performs called datevec() that performs this operation. I find it much easier to index datetime or perform calculations on other data when date and time are integers. For example, i generally need to calculate averages, std, etc based on specific months, years, days, and hours. Those calculations are extremely simple when I can index an array of datetime integers. If there is no module to convert datetime to an array of integers, does anyone have an example of how i might index datetime using python datetime or numpy datetime64? In each case, I would need an array of datetime the same dimension as my data array. thanks -- jeremy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] New to Python
Hello, I very new to python and have been playing around with some simple code that I would eventually use in the real word. Below is the code I have created. I want the code to read data from a file called SIL633.txt and then output that data into another file called test.txt. below is my code: #! /usr/bin/env python # Read NRG Text File def readNRGtxt(): import numpy as np f1 = open('SIL633_original.txt','r') data = np.genfromtxt(f1,delimiter='\t',skip_header=141) f1.close f2 = open('test.txt','w') np.savetxt(f2,data,fmt='%6.2f',delimiter='\t') f2.close I'm running this on mac 10.5.8. When I run this script from the command line, there is no file output. However, if I remove the "def" statement in the script and run again from the command line, a test.txt file is output. I guess I'm trying to understand why using the def statement changes how the script operates and since it does, how do I correct this code so it runs from the command line with the def statement. thanks for any help -- jeremy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor